Add alternative enum assigned name mapping
[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 information to properly serialize
27  * and deserialize from Java YANG Binding data to NormalizedNode data.
28  *
29  * <p>
30  * Two core subtypes of codec context are available:
31  * <ul>
32  * <li>{@link LeafNodeCodecContext} - Context for nodes, which does not contain any nested YANG modeled substructures.
33  * </li>
34  * <li>{@link DataObjectCodecContext} - Context for nodes, which does contain nested YANG modeled substructures. This
35  * context nodes contains context for children nodes.</li>
36  * </ul>
37  */
38 abstract class NodeCodecContext<D extends DataObject> implements BindingCodecTreeNode<D> {
39
40     /**
41      * Returns Yang Instance Identifier Path Argument of current node.
42      *
43      * @return DOM Path Argument of node
44      */
45     protected abstract YangInstanceIdentifier.PathArgument getDomPathArgument();
46
47     /**
48      * Immutable factory, which provides access to runtime context, create leaf nodes and provides path argument codecs.
49      *
50      * <p>
51      * During lifetime of factory all calls for same arguments to method must return equal result (not necessary same
52      * instance of result).
53      */
54     protected interface CodecContextFactory {
55         /**
56          * Returns immutable runtime context associated with this factory.
57          *
58          * @return runtime context
59          */
60         BindingRuntimeContext getRuntimeContext();
61
62         /**
63          * Returns leaf nodes for supplied data container and parent class.
64          *
65          * @param type Binding type for which leaves should be loaded.
66          * @param schema  Instantiated schema of binding type.
67          * @return Map of local name to leaf node context.
68          */
69         ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(Class<?> type, DataNodeContainer schema);
70
71         /**
72          * Returns Path argument codec for list item.
73          *
74          * @param type Type of list item
75          * @param schema Schema of list item
76          * @return Path argument codec for supplied list item.
77          */
78         Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> getPathArgumentCodec(Class<?> type,
79                 ListSchemaNode schema);
80
81         DataObjectSerializer getEventStreamSerializer(Class<?> type);
82     }
83
84     /**
85      * Serializes supplied Binding Path Argument and adds all necessary YANG instance identifiers to supplied list.
86      *
87      * @param arg Binding Path Argument
88      * @param builder DOM Path argument.
89      */
90     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
91             final List<YangInstanceIdentifier.PathArgument> builder) {
92         if (builder != null) {
93             builder.add(getDomPathArgument());
94         }
95     }
96
97     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
98 }