Split out CodecContextFactory
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecPrototype.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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.Identifiable;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
23
24 // FIXME: abstract and sealed
25 non-sealed class DataObjectCodecPrototype<T extends RuntimeTypeContainer> extends DataContainerCodecPrototype<T> {
26     private final @NonNull NodeIdentifier yangArg;
27
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);
32     }
33
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);
38     }
39
40     @Override
41     final NodeIdentifier getYangArg() {
42         return yangArg;
43     }
44
45     @Override
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)
52                     .isEmpty()) {
53                 return new NonPresenceContainerNodeCodecContext(this);
54             }
55             return new ContainerNodeCodecContext(this);
56         } else if (type instanceof ListRuntimeType) {
57             return Identifiable.class.isAssignableFrom(getBindingClass())
58                     ? KeyedListNodeCodecContext.create((DataContainerCodecPrototype<ListRuntimeType>) this)
59                             : new ListNodeCodecContext(this);
60         } else if (type instanceof ChoiceRuntimeType) {
61             return new ChoiceNodeCodecContext(this);
62         }
63         throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + type);
64     }
65 }