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=189d443abcc9e26e1722f266db214af503f0e0a8;hb=refs%2Fchanges%2F45%2F98245%2F100;hp=2c16d7a49d4111ea614f62fe0dbe04bbf616f8d7;hpb=259957ffe47414cc197f4a819361e8a6dcf50b19;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 2c16d7a49d..189d443abc 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,36 +7,72 @@ */ 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 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; +import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement; + +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); + } + } + + 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); + } + } -final class KeyedListNodeCodecContext> extends ListNodeCodecContext { private final IdentifiableItemCodec codec; - KeyedListNodeCodecContext(final DataContainerCodecPrototype prototype) { - super(prototype, keyMethod(prototype.getBindingClass())); - this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema()); + KeyedListNodeCodecContext(final DataContainerCodecPrototype prototype, + final Method keyMethod, final IdentifiableItemCodec codec) { + super(prototype, keyMethod); + this.codec = requireNonNull(codec); } - private static Method keyMethod(final Class bindingClass) { + @SuppressWarnings("rawtypes") + static KeyedListNodeCodecContext create(final DataContainerCodecPrototype prototype) { + final Class bindingClass = prototype.getBindingClass(); + final Method keyMethod; try { - // This just verifies the method is present - return bindingClass.getMethod(IDENTIFIABLE_KEY_NAME); + 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().findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class) + .orElse(Ordering.SYSTEM) == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec) + : new Ordered<>(prototype, keyMethod, codec); } @Override @@ -59,16 +95,6 @@ final class KeyedListNodeCodecContext> ex } } - @Override - @SuppressWarnings("rawtypes") - Object getBindingChildValue(final NormalizedNodeContainer dom, final int offset) { - if (offset == CodecDataObjectCustomizer.KEY_OFFSET && dom instanceof MapEntryNode) { - NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier(); - return codec.deserialize(identifier).getKey(); - } - return super.getBindingChildValue(dom, offset); - } - @Override protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) { if (domArg instanceof NodeIdentifierWithPredicates) { @@ -82,6 +108,10 @@ final class KeyedListNodeCodecContext> ex return codec.serialize(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) {