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