X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FKeyedListNodeCodecContext.java;h=5367d6d6738e1c6af0af1b10b1aa00a4d6d9fe6b;hb=984dfcd854a006724cbf0b20efc8bac6094bad48;hp=d8cac9d4660d89cc2e022c900d835f30785688e1;hpb=afcffc25b637b243daa0e7aad6a80048cfdeb21d;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/KeyedListNodeCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/KeyedListNodeCodecContext.java index d8cac9d466..5367d6d673 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/KeyedListNodeCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/KeyedListNodeCodecContext.java @@ -7,49 +7,86 @@ */ package org.opendaylight.mdsal.binding.dom.codec.impl; +import static java.util.Objects.requireNonNull; +import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.IDENTIFIABLE_KEY_NAME; + import java.lang.reflect.Method; import java.util.List; -import org.opendaylight.yangtools.concepts.Codec; +import java.util.Map; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Identifiable; 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.common.Ordering; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; -final class KeyedListNodeCodecContext> extends ListNodeCodecContext { - private final Codec> codec; - private final Method keyGetter; +abstract class KeyedListNodeCodecContext, D extends DataObject & Identifiable> + extends ListNodeCodecContext { + private static final class Ordered, D extends DataObject & Identifiable> + extends KeyedListNodeCodecContext { + Ordered(final DataContainerCodecPrototype prototype, final Method keyMethod, + final IdentifiableItemCodec codec) { + super(prototype, keyMethod, codec); + } + } - KeyedListNodeCodecContext(final DataContainerCodecPrototype prototype) { - super(prototype); + static final class Unordered, D extends DataObject & Identifiable> + extends KeyedListNodeCodecContext { + Unordered(final DataContainerCodecPrototype prototype, final Method keyMethod, + final IdentifiableItemCodec codec) { + super(prototype, keyMethod, codec); + } + + @Override + Map fromMap(final MapNode map, final int size) { + return LazyBindingMap.create(this, map, size); + } + } - this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema()); + private final IdentifiableItemCodec codec; + + KeyedListNodeCodecContext(final DataContainerCodecPrototype prototype, + final Method keyMethod, final IdentifiableItemCodec codec) { + super(prototype, keyMethod); + this.codec = requireNonNull(codec); + } + + @SuppressWarnings("rawtypes") + static KeyedListNodeCodecContext create(final DataContainerCodecPrototype prototype) { + final Class bindingClass = prototype.getBindingClass(); + final Method keyMethod; try { - this.keyGetter = getBindingClass().getMethod("getKey"); + keyMethod = bindingClass.getMethod(IDENTIFIABLE_KEY_NAME); } catch (NoSuchMethodException e) { throw new IllegalStateException("Required method not available", e); } + + final ListRuntimeType type = prototype.getType(); + final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass, type); + + return type.statement().ordering() == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec) + : new Ordered<>(prototype, keyMethod, codec); } @Override - protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg, final List builder) { + protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg, + final List builder) { /* - * DOM Instance Identifier for list is always represent by two - * entries one for map and one for children. This is also true for - * wildcarded instance identifiers + * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This + * is also true for wildcarded instance identifiers */ if (builder == null) { return; } super.addYangPathArgument(arg, builder); - if (arg instanceof IdentifiableItem) { - builder.add(codec.serialize((IdentifiableItem) arg)); + if (arg instanceof IdentifiableItem) { + builder.add(codec.bindingToDom((IdentifiableItem) arg)); } else { // Adding wildcarded super.addYangPathArgument(arg, builder); @@ -57,44 +94,29 @@ final class KeyedListNodeCodecContext> ex } @Override - @SuppressWarnings("rawtypes") - Object getBindingChildValue(final Method method, final NormalizedNodeContainer dom) { - if (dom instanceof MapEntryNode && keyGetter.equals(method)) { - NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier(); - return codec.deserialize(identifier).getKey(); - } else { - return super.getBindingChildValue(method, dom); - } - } - - @Override - protected InstanceIdentifier.PathArgument getBindingPathArgument(final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument domArg) { - if (domArg instanceof NodeIdentifierWithPredicates) { - return codec.deserialize((NodeIdentifierWithPredicates) domArg); - } - return super.getBindingPathArgument(domArg); + protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) { + return domArg instanceof NodeIdentifierWithPredicates + ? codec.domToBinding((NodeIdentifierWithPredicates) domArg) : super.getBindingPathArgument(domArg); } @SuppressWarnings({ "rawtypes", "unchecked" }) NodeIdentifierWithPredicates serialize(final Identifier key) { - return codec.serialize(new IdentifiableItem(getBindingClass(), key)); + return codec.bindingToDom(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key)); + } + + @NonNull Identifier deserialize(final NodeIdentifierWithPredicates arg) { + return codec.deserializeIdentifier(arg); } @Override public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) { - if(arg instanceof IdentifiableItem) { - return codec.serialize((IdentifiableItem) arg); - } - return super.serializePathArgument(arg); + return arg instanceof IdentifiableItem + ? codec.bindingToDom((IdentifiableItem) arg) : super.serializePathArgument(arg); } @Override public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) { - if(arg instanceof NodeIdentifierWithPredicates) { - return codec.deserialize((NodeIdentifierWithPredicates) arg); - } - return super.deserializePathArgument(arg); + return arg instanceof NodeIdentifierWithPredicates + ? codec.domToBinding((NodeIdentifierWithPredicates) arg) : super.deserializePathArgument(arg); } - - }