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