Binding2 runtime - Codecs impl #2
[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.codecs.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  *
29  * Location specific context for schema nodes, which contains codec specific
30  * information to properly serialize / deserialize from Java YANG Binding data
31  * to NormalizedNode data.
32  *
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 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     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
51
52     /**
53      *
54      * Immutable factory, which provides access to runtime context,
55      * create leaf nodes and provides path argument codecs.
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 Path argument codec for list item
79          *
80          * @param type Type of list item
81          * @param schema Schema of list item
82          * @return Path argument codec for supplied list item.
83          */
84         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
85            ListSchemaNode schema);
86
87         TreeNodeSerializer getEventStreamSerializer(Class<?> type);
88     }
89
90     /**
91      *
92      * Serializes supplied Binding Path Argument
93      * and adds all necessary YANG instance identifiers to supplied list.
94      *
95      * @param arg Binding Path Argument
96      * @param builder DOM Path argument.
97      */
98     @SuppressWarnings("rawtypes")
99     protected void addYangPathArgument(final TreeArgument arg,
100             final List<YangInstanceIdentifier.PathArgument> builder) {
101         if (builder != null) {
102             builder.add(getDomPathArgument());
103         }
104     }
105
106     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
107 }