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