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.dom.codec.impl.NodeCodecContext.CodecContextFactory;
14 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
15 import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
16 import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType;
17 import org.opendaylight.mdsal.binding.runtime.api.ContainerRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
19 import org.opendaylight.mdsal.binding.runtime.api.RuntimeTypeContainer;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.Identifiable;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
26 final class DataObjectCodecPrototype<T extends RuntimeTypeContainer> extends DataContainerCodecPrototype<T> {
27 private final @NonNull NodeIdentifier yangArg;
29 @SuppressWarnings("unchecked")
30 DataObjectCodecPrototype(final Class<?> cls, final NodeIdentifier yangArg, final T type,
31 final CodecContextFactory factory) {
32 this(Item.of((Class<? extends DataObject>) cls), yangArg, type, factory);
35 DataObjectCodecPrototype(final Item<?> bindingArg, final NodeIdentifier yangArg, final T type,
36 final CodecContextFactory factory) {
37 super(bindingArg, yangArg.getNodeType().getModule(), type, factory);
38 this.yangArg = requireNonNull(yangArg);
42 NodeIdentifier getYangArg() {
47 @SuppressWarnings({ "rawtypes", "unchecked" })
48 DataContainerCodecContext<?, T> createInstance() {
49 final var type = getType();
50 if (type instanceof ContainerLikeRuntimeType containerLike) {
51 if (containerLike instanceof ContainerRuntimeType container
52 && container.statement().findFirstEffectiveSubstatement(PresenceEffectiveStatement.class)
54 return new NonPresenceContainerNodeCodecContext(this);
56 return new ContainerNodeCodecContext(this);
57 } else if (type instanceof ListRuntimeType) {
58 return Identifiable.class.isAssignableFrom(getBindingClass())
59 ? KeyedListNodeCodecContext.create((DataContainerCodecPrototype<ListRuntimeType>) this)
60 : new ListNodeCodecContext(this);
61 } else if (type instanceof ChoiceRuntimeType) {
62 return new ChoiceNodeCodecContext(this);
63 } else if (type instanceof CaseRuntimeType) {
64 return new CaseNodeCodecContext(this);
66 throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + type);