61b26a42be486e24385bf30afcdf51242f50eff0
[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.loader.CodecClassLoader;
17 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
18 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
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.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24
25 /**
26  * Location specific context for schema nodes, which contains codec specific information to properly serialize
27  * and deserialize from Java YANG Binding data to NormalizedNode data.
28  *
29  * <p>
30  * Two core subtypes of codec context are available:
31  * <ul>
32  * <li>{@link ValueNodeCodecContext} - Context for nodes, which does not contain any nested YANG modeled substructures.
33  * </li>
34  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain nested YANG modeled substructures. This
35  * context nodes contains context for children nodes.</li>
36  * </ul>
37  */
38 abstract class NodeCodecContext implements BindingCodecTreeNode {
39     /**
40      * Transition runtime constant to allow choosing between legacy mutable lists and immutable lists.
41      */
42     // FIXME: 5.0.0: MDSAL-446: remove this knob completely
43     static final boolean COMPAT_MUTABLE_LISTS;
44
45     static {
46         final String propVal = System.getProperty("org.opendaylight.mdsal.binding.dom.codec.impl.compat-mutable-lists");
47         // FIXME: 5.0.0: MDSAL-446: if not above, flip the default to opt-out
48         COMPAT_MUTABLE_LISTS = propVal == null ? true : Boolean.parseBoolean(propVal);
49     }
50
51     /**
52      * Returns Yang Instance Identifier Path Argument of current node.
53      *
54      * @return DOM Path Argument of node
55      */
56     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
57
58     /**
59      * Immutable factory, which provides access to runtime context, create leaf nodes and provides path argument codecs.
60      *
61      * <p>
62      * During lifetime of factory all calls for same arguments to method must return equal result (not necessary same
63      * instance of result).
64      */
65     protected interface CodecContextFactory {
66         /**
67          * Returns immutable runtime context associated with this factory.
68          *
69          * @return runtime context
70          */
71         BindingRuntimeContext getRuntimeContext();
72
73         /**
74          * Returns leaf nodes for supplied data container and parent class.
75          *
76          * @param type Binding type for which leaves should be loaded.
77          * @param schema  Instantiated schema of binding type.
78          * @return Map of local name to leaf node context.
79          */
80         ImmutableMap<Method, ValueNodeCodecContext> getLeafNodes(Class<?> type, DataNodeContainer schema);
81
82         /**
83          * Returns Path argument codec for list item.
84          *
85          * @param type Type of list item
86          * @param schema Schema of list item
87          * @return Path argument codec for supplied list item.
88          */
89         IdentifiableItemCodec getPathArgumentCodec(Class<?> type, ListSchemaNode schema);
90
91         /**
92          * Return the codec loader associated with this factory.
93          *
94          * @return A codec loader instance
95          */
96         @NonNull CodecClassLoader getLoader();
97
98         @NonNull DataObjectStreamer<?> getDataObjectSerializer(Class<?> type);
99
100         DataObjectSerializer getEventStreamSerializer(Class<?> type);
101     }
102
103     /**
104      * Serializes supplied Binding Path Argument and adds all necessary YANG instance identifiers to supplied list.
105      *
106      * @param arg Binding Path Argument
107      * @param builder DOM Path argument.
108      */
109     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
110             final List<YangInstanceIdentifier.PathArgument> builder) {
111         if (builder != null) {
112             builder.add(getDomPathArgument());
113         }
114     }
115
116     /**
117      * Return the default value object. Implementations of this method are explicitly allowed to throw unchecked
118      * exceptions, which are propagated as-is upwards the stack.
119      *
120      * @return The default value object, or null if the default value is not defined.
121      */
122     @Nullable Object defaultObject() {
123         return null;
124     }
125
126     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
127 }