Refactor DataObjectCodecContext.getBindingChildValue()
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / 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.mdsal.binding.dom.codec.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableCollection;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.Set;
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.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
41
42 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
43
44     private final YangInstanceIdentifier.PathArgument yangIdentifier;
45     private final Codec<Object, Object> valueCodec;
46     private final Method getter;
47     private final DataSchemaNode schema;
48     private final Object defaultObject;
49
50     LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
51                 final SchemaContext schemaContext) {
52         this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
53         this.valueCodec = requireNonNull(codec);
54         this.getter = getter;
55         this.schema = requireNonNull(schema);
56
57         this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
58     }
59
60     private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
61                                               final SchemaContext schemaContext) {
62         if (schema instanceof LeafSchemaNode) {
63             Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
64             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
65             if (defaultValue.isPresent()) {
66                 if (type instanceof IdentityrefTypeDefinition) {
67                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
68                 }
69                 return domValueFromString(codec, type, defaultValue.get());
70             }
71
72             while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
73                 type = type.getBaseType();
74             }
75
76             defaultValue = type.getDefaultValue();
77             if (defaultValue.isPresent()) {
78                 if (type instanceof IdentityrefTypeDefinition) {
79                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
80                 }
81                 return domValueFromString(codec, type, defaultValue);
82             }
83         }
84         return null;
85     }
86
87     private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
88                                                   final String defaultValue, final SchemaContext schemaContext) {
89         int prefixEndIndex = defaultValue.indexOf(':');
90         QName qname;
91         if (prefixEndIndex != -1) {
92             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
93
94             Module module = schemaContext.findModule(schema.getQName().getModule()).get();
95             if (module.getPrefix().equals(defaultValuePrefix)) {
96                 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
97                 return codec.deserialize(qname);
98             }
99
100             Set<ModuleImport> imports = module.getImports();
101             for (ModuleImport moduleImport : imports) {
102                 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
103                     Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
104                         moduleImport.getRevision()).get();
105                     qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
106                     return codec.deserialize(qname);
107                 }
108             }
109             return null;
110         }
111
112         qname = QName.create(schema.getQName(), defaultValue);
113         return codec.deserialize(qname);
114     }
115
116     private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
117             final Object defaultValue) {
118         TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
119         if (typeDefAwareCodec != null) {
120             Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
121             return codec.deserialize(castedDefaultValue);
122         }
123         // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
124         // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
125         return null;
126     }
127
128     @Override
129     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
130         return yangIdentifier;
131     }
132
133     protected Codec<Object, Object> getValueCodec() {
134         return valueCodec;
135     }
136
137     @Override
138     public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
139         throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
140     }
141
142     @Override
143     public NodeCodecContext<?> get() {
144         return this;
145     }
146
147     Method getGetter() {
148         return getter;
149     }
150
151     @Override
152     public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
153             final List<YangInstanceIdentifier.PathArgument> builder) {
154         throw new IllegalArgumentException("Leaf does not have children");
155     }
156
157     @Override
158     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
159             final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
160         throw new UnsupportedOperationException("Leaves does not support caching codec.");
161     }
162
163     @Override
164     public Class<D> getBindingClass() {
165         throw new UnsupportedOperationException("Leaf does not have DataObject representation");
166     }
167
168     @Override
169     public NormalizedNode<?, ?> serialize(final D data) {
170         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
171     }
172
173     @Override
174     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
175         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
176     }
177
178     @Override
179     public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
180         throw new IllegalArgumentException("Leaf does not have children");
181     }
182
183     @Override
184     public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
185             final Class<E> childClass) {
186         throw new IllegalArgumentException("Leaf does not have children");
187     }
188
189     @Override
190     public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
191         throw new IllegalArgumentException("Leaf does not have children");
192     }
193
194     @Override
195     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
196         if (normalizedNode instanceof LeafNode<?>) {
197             return valueCodec.deserialize(normalizedNode.getValue());
198         }
199         if (normalizedNode instanceof LeafSetNode<?>) {
200             @SuppressWarnings("unchecked")
201             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
202             final List<Object> result = new ArrayList<>(domValues.size());
203             for (final LeafSetEntryNode<Object> valueNode : domValues) {
204                 result.add(valueCodec.deserialize(valueNode.getValue()));
205             }
206             return result;
207         }
208         return null;
209     }
210
211     @Override
212     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
213         checkArgument(getDomPathArgument().equals(arg));
214         return null;
215     }
216
217     @Override
218     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
219         return getDomPathArgument();
220     }
221
222     @Override
223     public DataSchemaNode getSchema() {
224         return schema;
225     }
226
227     @Override
228     public ChildAddressabilitySummary getChildAddressabilitySummary() {
229         return ChildAddressabilitySummary.UNADDRESSABLE;
230     }
231
232     @Override
233     Object defaultObject() {
234         return defaultObject;
235     }
236 }