Merge "BUG-869: added proper handling of nullable parameter"
[yangtools.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / 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.yangtools.binding.data.codec.impl;
9
10 import com.google.common.collect.ImmutableMap;
11
12 import java.util.List;
13
14 import org.opendaylight.yangtools.concepts.Codec;
15 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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  *
26  * Location specific context for schema nodes, which contains codec specific
27  * information to properly serialize / deserialize from Java YANG Binding data
28  * to NormalizedNode data.
29  *
30  * Two core subtypes of codec context are available:
31  * <ul>
32  * <li>{@link LeafNodeCodecContext} - Context for nodes, which does not contain
33  * any nested YANG modeled substructures.</li>
34  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain
35  * nested YANG modeled substructures. This context nodes contains context
36  * for children nodes.</li>
37  * </ul>
38  *
39  */
40 abstract class NodeCodecContext {
41
42     /**
43      * Returns Yang Instance Identifier Path Argument of current node
44      *
45      * @return DOM Path Argument of node
46      */
47     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
48
49     /**
50      *
51      * Immutable factory, which provides access to runtime context,
52      * create leaf nodes and provides path argument codecs.
53      * <p>
54      * During lifetime of factory all calls for same arguments to method must return
55      * equal result (not necessary same instance of result).
56      *
57      */
58     protected interface CodecContextFactory {
59         /**
60          * Returns immutable runtime context associated with this factory.
61          * @return runtime context
62          */
63         BindingRuntimeContext getRuntimeContext();
64
65         /**
66          * Returns leaf nodes for supplied data container and parent class.
67          *
68          * @param type Binding type for which leaves should be loaded.
69          * @param schema  Instantiated schema of binding type.
70          * @return Map of local name to leaf node context.
71          */
72         ImmutableMap<String, LeafNodeCodecContext> getLeafNodes(Class<?> type, DataNodeContainer schema);
73
74         /**
75          * Returns Path argument codec for list item
76          *
77          * @param type Type of list item
78          * @param schema Schema of list item
79          * @return Path argument codec for supplied list item.
80          */
81         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
82                 ListSchemaNode schema);
83     }
84
85     /**
86      *
87      * Serializes supplied Binding Path Argument
88      * and adds all necessary YANG instance identifiers to supplied list.
89      *
90      * @param arg Binding Path Argument
91      * @param builder DOM Path argument.
92      */
93     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
94             final List<YangInstanceIdentifier.PathArgument> builder) {
95         if (builder != null) {
96             builder.add(getDomPathArgument());
97         }
98     }
99
100     /**
101      * Return the data object for a normalized node
102      *
103      * @param normalizedNode Backing normalized node
104      * @return Data object
105      */
106     protected abstract Object dataFromNormalizedNode(NormalizedNode<?, ?> normalizedNode);
107 }