Convert mdsal-binding-dom-codec to a JPMS module
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / NodeCodecContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 com.google.common.collect.ImmutableMap;
11 import java.lang.reflect.Method;
12 import java.util.List;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
16 import org.opendaylight.mdsal.binding.dom.codec.impl.loader.CodecClassLoader;
17 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
18 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23
24 /**
25  * Location specific context for schema nodes, which contains codec specific information to properly serialize
26  * and deserialize from Java YANG Binding data to NormalizedNode data.
27  *
28  * <p>
29  * Two core subtypes of codec context are available:
30  * <ul>
31  * <li>{@link ValueNodeCodecContext} - Context for nodes, which does not contain any nested YANG modeled substructures.
32  * </li>
33  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain nested YANG modeled substructures. This
34  * context nodes contains context for children nodes.</li>
35  * </ul>
36  */
37 abstract class NodeCodecContext implements BindingCodecTreeNode {
38     /**
39      * Returns Yang Instance Identifier Path Argument of current node.
40      *
41      * @return DOM Path Argument of node
42      */
43     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
44
45     /**
46      * Immutable factory, which provides access to runtime context, create leaf nodes and provides path argument codecs.
47      *
48      * <p>
49      * During lifetime of factory all calls for same arguments to method must return equal result (not necessary same
50      * instance of result).
51      */
52     protected interface CodecContextFactory {
53         /**
54          * Returns immutable runtime context associated with this factory.
55          *
56          * @return runtime context
57          */
58         BindingRuntimeContext getRuntimeContext();
59
60         /**
61          * Returns leaf nodes for supplied data container and parent class.
62          *
63          * @param type Binding type for which leaves should be loaded.
64          * @param schema  Instantiated schema of binding type.
65          * @return Map of local name to leaf node context.
66          */
67         ImmutableMap<Method, ValueNodeCodecContext> getLeafNodes(Class<?> type, EffectiveStatement<?, ?> schema);
68
69         /**
70          * Returns Path argument codec for list item.
71          *
72          * @param listClz Type of list item
73          * @param type Schema of list item
74          * @return Path argument codec for supplied list item.
75          */
76         IdentifiableItemCodec getPathArgumentCodec(Class<?> listClz, ListRuntimeType type);
77
78         /**
79          * Return the codec loader associated with this factory.
80          *
81          * @return A codec loader instance
82          */
83         @NonNull CodecClassLoader getLoader();
84
85         @NonNull DataObjectStreamer<?> getDataObjectSerializer(Class<?> type);
86
87         DataObjectSerializer getEventStreamSerializer(Class<?> type);
88     }
89
90     /**
91      * Serializes supplied Binding Path Argument and adds all necessary YANG instance identifiers to supplied list.
92      *
93      * @param arg Binding Path Argument
94      * @param builder DOM Path argument.
95      */
96     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
97             final List<YangInstanceIdentifier.PathArgument> builder) {
98         if (builder != null) {
99             builder.add(getDomPathArgument());
100         }
101     }
102
103     /**
104      * Return the default value object. Implementations of this method are explicitly allowed to throw unchecked
105      * exceptions, which are propagated as-is upwards the stack.
106      *
107      * @return The default value object, or null if the default value is not defined.
108      */
109     @Nullable Object defaultObject() {
110         return null;
111     }
112
113     protected abstract Object deserializeObject(NormalizedNode normalizedNode);
114 }