Split out BindingDataContainerCodecTreeNode
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CommonDataObjectCodecContext.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.api.CommonDataObjectCodecTreeNode;
14 import org.opendaylight.mdsal.binding.runtime.api.RuntimeTypeContainer;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20
21 /**
22  * Base implementation of {@link CommonDataObjectCodecTreeNode}.
23  */
24 abstract sealed class CommonDataObjectCodecContext<D extends DataObject, T extends RuntimeTypeContainer>
25         extends DataContainerCodecContext<D, T> implements CommonDataObjectCodecTreeNode<D>
26         permits AbstractDataObjectCodecContext, ChoiceCodecContext {
27     final @NonNull CommonDataObjectCodecPrototype<T> prototype;
28
29     CommonDataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype) {
30         super(prototype.getType());
31         this.prototype = requireNonNull(prototype);
32     }
33
34     @SuppressWarnings("unchecked")
35     @Override
36     public final Class<D> getBindingClass() {
37         return Class.class.cast(prototype.getBindingClass());
38     }
39
40     @Override
41     protected final CodecContextFactory factory() {
42         return prototype.getFactory();
43     }
44
45     @Override
46     protected final T type() {
47         return prototype.getType();
48     }
49
50     @Override
51     protected NodeIdentifier getDomPathArgument() {
52         return prototype.getYangArg();
53     }
54
55     /**
56      * Returns deserialized Binding Path Argument from YANG instance identifier.
57      */
58     protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
59         return bindingArg();
60     }
61
62     protected final PathArgument bindingArg() {
63         return prototype.getBindingArg();
64     }
65
66     protected final QNameModule namespace() {
67         return prototype.getNamespace();
68     }
69 }