Cleaned up Java Binding code from YANG Tools
[mdsal.git] / binding / mdsal-binding-dom-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 import java.util.List;
12 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
13 import org.opendaylight.yangtools.concepts.Codec;
14 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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  *
27  * Location specific context for schema nodes, which contains codec specific
28  * information to properly serialize / deserialize from Java YANG Binding data
29  * to NormalizedNode data.
30  *
31  * Two core subtypes of codec context are available:
32  * <ul>
33  * <li>{@link LeafNodeCodecContext} - Context for nodes, which does not contain
34  * any nested YANG modeled substructures.</li>
35  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain
36  * nested YANG modeled substructures. This context nodes contains context
37  * for children nodes.</li>
38  * </ul>
39  *
40  */
41 abstract class NodeCodecContext<D extends DataObject> implements BindingCodecTreeNode<D> {
42
43     /**
44      * Returns Yang Instance Identifier Path Argument of current node
45      *
46      * @return DOM Path Argument of node
47      */
48     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
49
50     /**
51      *
52      * Immutable factory, which provides access to runtime context,
53      * create leaf nodes and provides path argument codecs.
54      * <p>
55      * During lifetime of factory all calls for same arguments to method must return
56      * equal result (not necessary same instance of result).
57      *
58      */
59     protected interface CodecContextFactory {
60         /**
61          * Returns immutable runtime context associated with this factory.
62          * @return runtime context
63          */
64         BindingRuntimeContext getRuntimeContext();
65
66         /**
67          * Returns leaf nodes for supplied data container and parent class.
68          *
69          * @param type Binding type for which leaves should be loaded.
70          * @param schema  Instantiated schema of binding type.
71          * @return Map of local name to leaf node context.
72          */
73         ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(Class<?> type, DataNodeContainer schema);
74
75         /**
76          * Returns Path argument codec for list item
77          *
78          * @param type Type of list item
79          * @param schema Schema of list item
80          * @return Path argument codec for supplied list item.
81          */
82         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
83                 ListSchemaNode schema);
84
85         DataObjectSerializer getEventStreamSerializer(Class<?> type);
86     }
87
88     /**
89      *
90      * Serializes supplied Binding Path Argument
91      * 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     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
104 }