c89fdf72b5de9bfa4d90190aeaa237d210217984
[yangtools.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.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
14 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
15 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21
22 /**
23  * Location specific context for schema nodes, which contains codec specific information to properly serialize
24  * and deserialize from Java YANG Binding data to NormalizedNode data.
25  *
26  * <p>
27  * Two core subtypes of codec context are available:
28  * <ul>
29  * <li>{@link LeafNodeCodecContext} - Context for nodes, which does not contain any nested YANG modeled substructures.
30  * </li>
31  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain nested YANG modeled substructures. This
32  * context nodes contains context for children nodes.</li>
33  * </ul>
34  */
35 abstract class NodeCodecContext implements BindingCodecTreeNode {
36
37     /**
38      * Returns Yang Instance Identifier Path Argument of current node.
39      *
40      * @return DOM Path Argument of node
41      */
42     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
43
44     /**
45      * Immutable factory, which provides access to runtime context, create leaf nodes and provides path argument codecs.
46      *
47      * <p>
48      * During lifetime of factory all calls for same arguments to method must return equal result (not necessary same
49      * instance of result).
50      */
51     protected interface CodecContextFactory {
52         /**
53          * Returns immutable runtime context associated with this factory.
54          *
55          * @return runtime context
56          */
57         BindingRuntimeContext getRuntimeContext();
58
59         /**
60          * Returns leaf nodes for supplied data container and parent class.
61          *
62          * @param type Binding type for which leaves should be loaded.
63          * @param schema  Instantiated schema of binding type.
64          * @return Map of local name to leaf node context.
65          */
66         ImmutableMap<String, LeafNodeCodecContext> getLeafNodes(Class<?> type, DataNodeContainer schema);
67
68         /**
69          * Returns Path argument codec for list item.
70          *
71          * @param type Type of list item
72          * @param schema Schema of list item
73          * @return Path argument codec for supplied list item.
74          */
75         IdentifiableItemCodec getPathArgumentCodec(Class<?> type, ListSchemaNode schema);
76
77         DataObjectSerializer getEventStreamSerializer(Class<?> type);
78     }
79
80     /**
81      * Serializes supplied Binding Path Argument and adds all necessary YANG instance identifiers to supplied list.
82      *
83      * @param arg Binding Path Argument
84      * @param builder DOM Path argument.
85      */
86     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
87             final List<YangInstanceIdentifier.PathArgument> builder) {
88         if (builder != null) {
89             builder.add(getDomPathArgument());
90         }
91     }
92
93     /**
94      * Return the default value object. Implementations of this method are explicitly allowed to throw unchecked
95      * exceptions, which are propagated as-is upwards the stack.
96      *
97      * @return The default value object, or null if the default value is not defined.
98      */
99     @Nullable Object defaultObject() {
100         return null;
101     }
102
103     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
104 }