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.InstanceIdentifier.Item;
21 import org.opendaylight.yangtools.yang.binding.Key;
22 import org.opendaylight.yangtools.yang.binding.KeyAware;
23 import org.opendaylight.yangtools.yang.binding.contract.Naming;
24 import org.opendaylight.yangtools.yang.common.Ordering;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 abstract sealed class MapCodecContext<I extends Key<D>, D extends DataObject & KeyAware<I>>
30 extends ListCodecContext<D> {
31 private static final class Ordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
32 extends MapCodecContext<I, D> {
33 Ordered(final MapCodecPrototype prototype, final Method keyMethod, final IdentifiableItemCodec codec) {
34 super(prototype, keyMethod, codec);
38 static final class Unordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
39 extends MapCodecContext<I, D> {
40 private Unordered(final MapCodecPrototype 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.of(this, map, size);
51 private final IdentifiableItemCodec codec;
53 private MapCodecContext(final MapCodecPrototype prototype, final Method keyMethod,
54 final IdentifiableItemCodec codec) {
55 super(prototype, keyMethod);
56 this.codec = requireNonNull(codec);
59 static @NonNull MapCodecContext<?, ?> of(final Class<? extends DataObject> cls, final ListRuntimeType type,
60 final CodecContextFactory factory) {
61 return of(new MapCodecPrototype(Item.of(cls), type, factory));
64 static @NonNull MapCodecContext<?, ?> of(final MapCodecPrototype prototype) {
65 final var bindingClass = prototype.getBindingClass();
66 final Method keyMethod;
68 keyMethod = bindingClass.getMethod(Naming.KEY_AWARE_KEY_NAME);
69 } catch (NoSuchMethodException e) {
70 throw new IllegalStateException("Required method not available", e);
73 final var type = prototype.getType();
74 final var codec = prototype.getFactory().getPathArgumentCodec(bindingClass, type);
76 return type.statement().ordering() == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec)
77 : new Ordered<>(prototype, keyMethod, codec);
81 void addYangPathArgument(final List<YangInstanceIdentifier.PathArgument> builder,
82 final InstanceIdentifier.PathArgument arg) {
84 * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
85 * is also true for wildcarded instance identifiers
87 final var yangArg = getDomPathArgument();
90 if (arg instanceof IdentifiableItem<?, ?> identifiable) {
91 builder.add(codec.bindingToDom(identifiable));
99 protected final InstanceIdentifier.PathArgument getBindingPathArgument(
100 final YangInstanceIdentifier.PathArgument domArg) {
101 return domArg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
102 : super.getBindingPathArgument(domArg);
105 @SuppressWarnings({ "rawtypes", "unchecked" })
106 final NodeIdentifierWithPredicates serialize(final Key<?> key) {
107 return codec.bindingToDom(IdentifiableItem.of((Class)getBindingClass(), (Key)key));
110 final @NonNull Key<?> deserialize(final @NonNull NodeIdentifierWithPredicates arg) {
111 return codec.deserializeIdentifier(arg);
115 public final YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
116 return arg instanceof IdentifiableItem<?, ?> identifiable ? codec.bindingToDom(identifiable)
117 : super.serializePathArgument(arg);
121 public final InstanceIdentifier.PathArgument deserializePathArgument(
122 final YangInstanceIdentifier.PathArgument arg) {
123 return arg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
124 : super.deserializePathArgument(arg);