2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
12 import java.lang.reflect.Method;
13 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
20 import org.opendaylight.yangtools.yang.binding.Key;
21 import org.opendaylight.yangtools.yang.binding.KeyAware;
22 import org.opendaylight.yangtools.yang.binding.contract.Naming;
23 import org.opendaylight.yangtools.yang.common.Ordering;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 abstract sealed class KeyedListNodeCodecContext<I extends Key<D>, D extends DataObject & KeyAware<I>>
29 extends ListNodeCodecContext<D> {
30 private static final class Ordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
31 extends KeyedListNodeCodecContext<I, D> {
32 Ordered(final DataContainerCodecPrototype<ListRuntimeType> prototype, final Method keyMethod,
33 final IdentifiableItemCodec codec) {
34 super(prototype, keyMethod, codec);
38 static final class Unordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
39 extends KeyedListNodeCodecContext<I, D> {
40 Unordered(final DataContainerCodecPrototype<ListRuntimeType> prototype, final Method keyMethod,
41 final IdentifiableItemCodec codec) {
42 super(prototype, keyMethod, codec);
46 Map<I, D> fromMap(final MapNode map, final int size) {
47 return LazyBindingMap.create(this, map, size);
51 private final IdentifiableItemCodec codec;
53 KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype,
54 final Method keyMethod, final IdentifiableItemCodec codec) {
55 super(prototype, keyMethod);
56 this.codec = requireNonNull(codec);
59 @SuppressWarnings("rawtypes")
60 static KeyedListNodeCodecContext create(final DataContainerCodecPrototype<ListRuntimeType> prototype) {
61 final Class<?> bindingClass = prototype.getBindingClass();
62 final Method keyMethod;
64 keyMethod = bindingClass.getMethod(Naming.KEY_AWARE_KEY_NAME);
65 } catch (NoSuchMethodException e) {
66 throw new IllegalStateException("Required method not available", e);
69 final ListRuntimeType type = prototype.getType();
70 final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass, type);
72 return type.statement().ordering() == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec)
73 : new Ordered<>(prototype, keyMethod, codec);
77 void addYangPathArgument(final List<YangInstanceIdentifier.PathArgument> builder,
78 final InstanceIdentifier.PathArgument arg) {
80 * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
81 * is also true for wildcarded instance identifiers
83 final var yangArg = getDomPathArgument();
86 if (arg instanceof IdentifiableItem<?, ?> identifiable) {
87 builder.add(codec.bindingToDom(identifiable));
95 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
96 return domArg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
97 : super.getBindingPathArgument(domArg);
100 @SuppressWarnings({ "rawtypes", "unchecked" })
101 NodeIdentifierWithPredicates serialize(final Key<?> key) {
102 return codec.bindingToDom(IdentifiableItem.of((Class)getBindingClass(), (Key)key));
105 @NonNull Key<?> deserialize(final @NonNull NodeIdentifierWithPredicates arg) {
106 return codec.deserializeIdentifier(arg);
110 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
111 return arg instanceof IdentifiableItem<?, ?> identifiable ? codec.bindingToDom(identifiable)
112 : super.serializePathArgument(arg);
116 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
117 return arg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
118 : super.deserializePathArgument(arg);