Merge branch 'mdsal-trace' from controller
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / base / NodeCodecContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
15 import org.opendaylight.mdsal.binding.javav2.runtime.context.BindingRuntimeContext;
16 import org.opendaylight.mdsal.binding.javav2.spec.base.IdentifiableItem;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
20 import org.opendaylight.yangtools.concepts.Codec;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26
27 /**
28  * Location specific context for schema nodes, which contains codec specific
29  * information to properly serialize / deserialize from Java YANG Binding data
30  * to NormalizedNode data.
31  *
32  * <p>
33  * Two core subtypes of codec context are available:
34  * <ul>
35  * <li>{@link LeafNodeCodecContext} - Context for nodes, which does not contain
36  * any nested YANG modeled substructures.</li>
37  * <li>{@link TreeNodeCodecContext} - Context for nodes, which does contain
38  * nested YANG modeled substructures. This context nodes contains context
39  * for children nodes.</li>
40  * </ul>
41  *
42  */
43 @Beta
44 public abstract class NodeCodecContext<D extends TreeNode> implements BindingTreeNodeCodec<D> {
45     /**
46      * Returns Yang Instance Identifier Path Argument of current node.
47      *
48      * @return DOM Path Argument of node
49      */
50     public abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
51
52     /**
53      * Immutable factory, which provides access to runtime context,
54      * create leaf nodes and provides path argument codecs.
55      *
56      * <p>
57      * During lifetime of factory all calls for same arguments to method must return
58      * equal result (not necessary same instance of result).
59      *
60      */
61     protected interface CodecContextFactory {
62         /**
63          * Returns immutable runtime context associated with this factory.
64          * @return runtime context
65          */
66         BindingRuntimeContext getRuntimeContext();
67
68         /**
69          * Returns leaf nodes for supplied data container and parent class.
70          *
71          * @param type Binding type for which leaves should be loaded.
72          * @param schema  Instantiated schema of binding type.
73          * @return Map of local name to leaf node context.
74          */
75         ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(Class<?> type, DataNodeContainer schema);
76
77         /**
78          * Returns anyxml nodes for supplied data container and parent class.
79          *
80          * @param type Binding type for which anyxml should be loaded.
81          * @param schema  Instantiated schema of binding type.
82          * @return Map of local name to anyxml node context.
83          */
84         ImmutableMap<String, AnyxmlNodeCodecContext<?>> getAnyxmlNodes(Class<?> type, DataNodeContainer schema);
85
86         /**
87          * Returns Path argument codec for list item.
88          *
89          * @param type Type of list item
90          * @param schema Schema of list item
91          * @return Path argument codec for supplied list item.
92          */
93         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
94             ListSchemaNode schema);
95
96         TreeNodeSerializer getEventStreamSerializer(Class<?> type);
97     }
98
99     /**
100      * Serializes supplied Binding Path Argument
101      * and adds all necessary YANG instance identifiers to supplied list.
102      *
103      * @param arg Binding Path Argument
104      * @param builder DOM Path argument.
105      */
106     @SuppressWarnings("rawtypes")
107     protected void addYangPathArgument(final TreeArgument arg,
108             final List<YangInstanceIdentifier.PathArgument> builder) {
109         if (builder != null) {
110             builder.add(getDomPathArgument());
111         }
112     }
113
114     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
115 }