Merge "Bug 1586: Fixed missing processing of single qouted strings."
[yangtools.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / DataContainerCodecContext.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.yangtools.binding.data.codec.impl;
9
10 import com.google.common.base.Optional;
11 import java.util.List;
12 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
13 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17
18 abstract class DataContainerCodecContext<T> extends NodeCodecContext {
19
20     private final DataContainerCodecPrototype<T> prototype;
21
22     protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
23         this.prototype = prototype;
24     }
25
26     protected final T schema() {
27         return prototype.getSchema();
28     }
29
30     protected final QNameModule namespace() {
31         return prototype.getNamespace();
32     }
33
34     protected final CodecContextFactory factory() {
35         return prototype.getFactory();
36     }
37
38     @Override
39     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
40         return prototype.getYangArg();
41     }
42
43     /**
44      * Returns nested node context using supplied YANG Instance Identifier
45      *
46      * @param arg Yang Instance Identifier Argument
47      * @return Context of child
48      * @throws IllegalArgumentException If supplied argument does not represent valid child.
49      */
50     protected abstract NodeCodecContext getYangIdentifierChild(final YangInstanceIdentifier.PathArgument arg);
51
52     /**
53      * Returns nested node context using supplied Binding Instance Identifier
54      * and adds YANG instance identifiers to supplied list.
55      *
56      * @param arg Binding Instance Identifier Argument
57      * @return Context of child
58      * @throws IllegalArgumentException If supplied argument does not represent valid child.
59      */
60     protected  DataContainerCodecContext<?> getIdentifierChild(final InstanceIdentifier.PathArgument arg,
61             final List<YangInstanceIdentifier.PathArgument> builder) {
62         final DataContainerCodecContext<?> child = getStreamChild(arg.getType());
63         if (builder != null) {
64             child.addYangPathArgument(arg,builder);
65         }
66         return child;
67     }
68
69     /**
70      * Returns deserialized Binding Path Argument from YANG instance identifier.
71      *
72      * @param domArg
73      * @return
74      */
75     protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
76         return bindingArg();
77     }
78
79     protected final PathArgument bindingArg() {
80         return prototype.getBindingArg();
81     }
82
83     protected final Class<?> bindingClass() {
84         return prototype.getBindingClass();
85     }
86
87     /**
88      *
89      * Returns child context as if it was walked by
90      * {@link BindingStreamEventWriter}. This means that to enter case, one
91      * must issue getChild(ChoiceClass).getChild(CaseClass).
92      *
93      * @param childClass
94      * @return Context of child
95      * @throws IllegalArgumentException If supplied child class is not valid in specified context.
96      */
97     protected abstract DataContainerCodecContext<?> getStreamChild(final Class<?> childClass) throws IllegalArgumentException;
98
99     /**
100     *
101     * Returns child context as if it was walked by
102     * {@link BindingStreamEventWriter}. This means that to enter case, one
103     * must issue getChild(ChoiceClass).getChild(CaseClass).
104     *
105     * @param childClass
106     * @return Context of child or Optional absent is supplied class is not applicable in context.
107     */
108    protected abstract Optional<DataContainerCodecContext<?>> getPossibleStreamChild(final Class<?> childClass);
109
110     @Override
111     public String toString() {
112         return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
113     }
114
115 }