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