Generate DataObject codec implementation
[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.util.List;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
15 import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader;
16 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
17 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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     /**
40      * Returns Yang Instance Identifier Path Argument of current node.
41      *
42      * @return DOM Path Argument of node
43      */
44     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
45
46     /**
47      * Immutable factory, which provides access to runtime context, create leaf nodes and provides path argument codecs.
48      *
49      * <p>
50      * During lifetime of factory all calls for same arguments to method must return equal result (not necessary same
51      * instance of result).
52      */
53     protected interface CodecContextFactory {
54         /**
55          * Returns immutable runtime context associated with this factory.
56          *
57          * @return runtime context
58          */
59         BindingRuntimeContext getRuntimeContext();
60
61         /**
62          * Returns leaf nodes for supplied data container and parent class.
63          *
64          * @param type Binding type for which leaves should be loaded.
65          * @param schema  Instantiated schema of binding type.
66          * @return Map of local name to leaf node context.
67          */
68         ImmutableMap<String, ValueNodeCodecContext> getLeafNodes(Class<?> type, DataNodeContainer schema);
69
70         /**
71          * Returns Path argument codec for list item.
72          *
73          * @param type Type of list item
74          * @param schema Schema of list item
75          * @return Path argument codec for supplied list item.
76          */
77         IdentifiableItemCodec getPathArgumentCodec(Class<?> type, ListSchemaNode schema);
78
79         /**
80          * Return the codec loader associated with this factory.
81          *
82          * @return A codec loader instance
83          */
84         @NonNull CodecClassLoader getLoader();
85
86         DataObjectSerializer getEventStreamSerializer(Class<?> type);
87     }
88
89     /**
90      * Serializes supplied Binding Path Argument and adds all necessary YANG instance identifiers to supplied list.
91      *
92      * @param arg Binding Path Argument
93      * @param builder DOM Path argument.
94      */
95     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
96             final List<YangInstanceIdentifier.PathArgument> builder) {
97         if (builder != null) {
98             builder.add(getDomPathArgument());
99         }
100     }
101
102     /**
103      * Return the default value object. Implementations of this method are explicitly allowed to throw unchecked
104      * exceptions, which are propagated as-is upwards the stack.
105      *
106      * @return The default value object, or null if the default value is not defined.
107      */
108     @Nullable Object defaultObject() {
109         return null;
110     }
111
112     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
113 }