Split out SchemaRootCodecContext.Prototype
[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.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;
25
26 // FIXME: abstract and sealed
27 non-sealed class DataObjectCodecPrototype<T extends RuntimeTypeContainer> extends DataContainerCodecPrototype<T> {
28     private final @NonNull NodeIdentifier yangArg;
29
30     @SuppressWarnings("unchecked")
31     DataObjectCodecPrototype(final Class<?> cls, final NodeIdentifier yangArg, final T type,
32             final CodecContextFactory factory) {
33         this(Item.of((Class<? extends DataObject>) cls), yangArg, type, factory);
34     }
35
36     DataObjectCodecPrototype(final Item<?> bindingArg, final NodeIdentifier yangArg, final T type,
37             final CodecContextFactory factory) {
38         super(bindingArg, yangArg.getNodeType().getModule(), type, factory);
39         this.yangArg = requireNonNull(yangArg);
40     }
41
42     @Override
43     final NodeIdentifier getYangArg() {
44         return yangArg;
45     }
46
47     @Override
48     @SuppressWarnings({ "rawtypes", "unchecked" })
49     DataContainerCodecContext<?, T> createInstance() {
50         final var type = getType();
51         if (type instanceof ContainerLikeRuntimeType containerLike) {
52             if (containerLike instanceof ContainerRuntimeType container
53                 && container.statement().findFirstEffectiveSubstatement(PresenceEffectiveStatement.class)
54                     .isEmpty()) {
55                 return new NonPresenceContainerNodeCodecContext(this);
56             }
57             return new ContainerNodeCodecContext(this);
58         } else if (type instanceof ListRuntimeType) {
59             return Identifiable.class.isAssignableFrom(getBindingClass())
60                     ? KeyedListNodeCodecContext.create((DataContainerCodecPrototype<ListRuntimeType>) this)
61                             : new ListNodeCodecContext(this);
62         } else if (type instanceof ChoiceRuntimeType) {
63             return new ChoiceNodeCodecContext(this);
64         } else if (type instanceof CaseRuntimeType) {
65             return new CaseNodeCodecContext(this);
66         }
67         throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + type);
68     }
69 }