9e40fdad373d3df5725a61074883e5a2820578a9
[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.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
13 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
14 import org.opendaylight.yangtools.concepts.Codec;
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  * 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 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      *
50      * Immutable factory, which provides access to runtime context,
51      * create leaf nodes and provides path argument codecs.
52      * <p>
53      * During lifetime of factory all calls for same arguments to method must return
54      * equal result (not necessary same instance of result).
55      *
56      */
57     protected interface CodecContextFactory {
58         /**
59          * Returns immutable runtime context associated with this factory.
60          * @return runtime context
61          */
62         BindingRuntimeContext getRuntimeContext();
63
64         /**
65          * Returns leaf nodes for supplied data container and parent class.
66          *
67          * @param type Binding type for which leaves should be loaded.
68          * @param schema  Instantiated schema of binding type.
69          * @return Map of local name to leaf node context.
70          */
71         ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(Class<?> type, DataNodeContainer schema);
72
73         /**
74          * Returns Path argument codec for list item
75          *
76          * @param type Type of list item
77          * @param schema Schema of list item
78          * @return Path argument codec for supplied list item.
79          */
80         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
81                 ListSchemaNode schema);
82
83         DataObjectSerializer getEventStreamSerializer(Class<?> type);
84     }
85
86     /**
87      *
88      * Serializes supplied Binding Path Argument
89      * and adds all necessary YANG instance identifiers to supplied list.
90      *
91      * @param arg Binding Path Argument
92      * @param builder DOM Path argument.
93      */
94     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
95             final List<YangInstanceIdentifier.PathArgument> builder) {
96         if (builder != null) {
97             builder.add(getDomPathArgument());
98         }
99     }
100
101     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
102 }