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