2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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 org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
14 import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType;
15 import org.opendaylight.mdsal.binding.runtime.api.ContainerRuntimeType;
16 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
17 import org.opendaylight.mdsal.binding.runtime.api.RuntimeTypeContainer;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
20 import org.opendaylight.yangtools.yang.binding.KeyAware;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
24 // FIXME: abstract and sealed
25 non-sealed class DataObjectCodecPrototype<T extends RuntimeTypeContainer> extends DataContainerCodecPrototype<T> {
26 private final @NonNull NodeIdentifier yangArg;
28 @SuppressWarnings("unchecked")
29 DataObjectCodecPrototype(final Class<?> cls, final NodeIdentifier yangArg, final T type,
30 final CodecContextFactory factory) {
31 this(Item.of((Class<? extends DataObject>) cls), yangArg, type, factory);
34 DataObjectCodecPrototype(final Item<?> bindingArg, final NodeIdentifier yangArg, final T type,
35 final CodecContextFactory factory) {
36 super(bindingArg, yangArg.getNodeType().getModule(), type, factory);
37 this.yangArg = requireNonNull(yangArg);
41 final NodeIdentifier getYangArg() {
46 @SuppressWarnings({ "rawtypes", "unchecked" })
47 DataContainerCodecContext<?, T> createInstance() {
48 final var type = getType();
49 if (type instanceof ContainerLikeRuntimeType containerLike) {
50 if (containerLike instanceof ContainerRuntimeType container
51 && container.statement().findFirstEffectiveSubstatement(PresenceEffectiveStatement.class)
53 return new StructuralContainerCodecContext(this);
55 return new ContainerLikeCodecContext(this);
56 } else if (type instanceof ListRuntimeType) {
57 return KeyAware.class.isAssignableFrom(getBindingClass())
58 ? KeyedListNodeCodecContext.create((DataContainerCodecPrototype<ListRuntimeType>) this)
59 : new ListNodeCodecContext(this);
60 } else if (type instanceof ChoiceRuntimeType) {
61 return new ChoiceCodecContext(this);
63 throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + type);