BUG-4322: return default valued-object
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / LeafNodeCodecContext.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 com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableCollection;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
20 import org.opendaylight.yangtools.concepts.Codec;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IdentityEffectiveStatementImpl;
37
38 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
39
40     private final YangInstanceIdentifier.PathArgument yangIdentifier;
41     private final Codec<Object, Object> valueCodec;
42     private final Method getter;
43     private final DataSchemaNode schema;
44     private final Object defaultObject;
45
46     public LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter) {
47         this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
48         this.valueCodec = Preconditions.checkNotNull(codec);
49         this.getter = getter;
50         this.schema = Preconditions.checkNotNull(schema);
51
52         this.defaultObject = createDefaultObject(schema, valueCodec);
53     }
54
55     private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec) {
56         if (schema instanceof LeafSchemaNode) {
57             Object defaultValue = ((LeafSchemaNode) schema).getDefault();
58             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
59             if (defaultValue != null) {
60                 return domValueFromString(codec, type, defaultValue);
61             }
62             else {
63                 while (type.getBaseType() != null && type.getDefaultValue() == null) {
64                     type = type.getBaseType();
65                 }
66
67                 defaultValue = type.getDefaultValue();
68                 if (defaultValue != null) {
69                     if (defaultValue instanceof Boolean) {
70                         return codec.deserialize(defaultValue);
71                     }
72
73                     if (defaultValue instanceof IdentitySchemaNode) {
74                         defaultValue = ((IdentityEffectiveStatementImpl) defaultValue).argument();
75                         return codec.deserialize(defaultValue);
76                     }
77
78                     if (defaultValue instanceof List) {
79                         return codec.deserialize(defaultValue);
80                     }
81                     return domValueFromString(codec, type, defaultValue);
82                 }
83             }
84         }
85         return null;
86     }
87
88     private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
89     Object defaultValue) {
90         TypeDefinitionAwareCodec typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
91         Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
92         return codec.deserialize(castedDefaultValue);
93     }
94
95     @Override
96     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
97         return yangIdentifier;
98     }
99
100     protected Codec<Object, Object> getValueCodec() {
101         return valueCodec;
102     }
103
104     @Override
105     public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
106         throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
107     }
108
109     @Override
110     public NodeCodecContext<?> get() {
111         return this;
112     }
113
114     final Method getGetter() {
115         return getter;
116     }
117
118     @Override
119     public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
120             final List<YangInstanceIdentifier.PathArgument> builder) {
121         throw new IllegalArgumentException("Leaf does not have children");
122     }
123
124     @Override
125     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
126             final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
127         throw new UnsupportedOperationException("Leaves does not support caching codec.");
128     }
129
130     @Override
131     public Class<D> getBindingClass() {
132         throw new UnsupportedOperationException("Leaf does not have DataObject representation");
133     }
134
135     @Override
136     public NormalizedNode<?, ?> serialize(final D data) {
137         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
138     }
139
140     @Override
141     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
142         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
143     }
144
145     @Override
146     public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
147         throw new IllegalArgumentException("Leaf does not have children");
148     }
149
150     @Override
151     public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
152             final Class<E> childClass) {
153         throw new IllegalArgumentException("Leaf does not have children");
154     }
155
156     @Override
157     public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
158         throw new IllegalArgumentException("Leaf does not have children");
159     }
160
161     @Override
162     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
163         if (normalizedNode instanceof LeafNode<?>) {
164             return valueCodec.deserialize(normalizedNode.getValue());
165         }
166         if (normalizedNode instanceof LeafSetNode<?>) {
167             @SuppressWarnings("unchecked")
168             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
169             final List<Object> result = new ArrayList<>(domValues.size());
170             for (final LeafSetEntryNode<Object> valueNode : domValues) {
171                 result.add(valueCodec.deserialize(valueNode.getValue()));
172             }
173             return result;
174         }
175         return null;
176     }
177
178     @Override
179     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
180         Preconditions.checkArgument(getDomPathArgument().equals(arg));
181         return null;
182     }
183
184     @Override
185     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
186         return getDomPathArgument();
187     }
188
189     @Override
190     public Object getSchema() {
191         return schema;
192     }
193
194     /**
195      * Return the default value object.
196      *
197      * @return The default value object, or null if the default value is not defined.
198      */
199     @Nullable Object defaultObject() {
200         return defaultObject;
201     }
202 }