Adjust to yangtools-2.0.0 changes
[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 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             java.util.Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
63             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
64             if (defaultValue.isPresent()) {
65                 if (type instanceof IdentityrefTypeDefinition) {
66                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
67                 }
68                 return domValueFromString(codec, type, defaultValue.get());
69             }
70
71             while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
72                 type = type.getBaseType();
73             }
74
75             defaultValue = type.getDefaultValue();
76             if (defaultValue.isPresent()) {
77                 if (type instanceof IdentityrefTypeDefinition) {
78                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
79                 }
80                 return domValueFromString(codec, type, defaultValue);
81             }
82         }
83         return null;
84     }
85
86     private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
87                                                   final String defaultValue, final SchemaContext schemaContext) {
88         int prefixEndIndex = defaultValue.indexOf(':');
89         QName qname;
90         if (prefixEndIndex != -1) {
91             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
92
93             Module module = schemaContext.findModule(schema.getQName().getModule()).get();
94             if (module.getPrefix().equals(defaultValuePrefix)) {
95                 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
96                 return codec.deserialize(qname);
97             }
98
99             Set<ModuleImport> imports = module.getImports();
100             for (ModuleImport moduleImport : imports) {
101                 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
102                     Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
103                         moduleImport.getRevision()).get();
104                     qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
105                     return codec.deserialize(qname);
106                 }
107             }
108             return null;
109         }
110
111         qname = QName.create(schema.getQName(), defaultValue);
112         return codec.deserialize(qname);
113     }
114
115     private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
116             final Object defaultValue) {
117         TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
118         if (typeDefAwareCodec != null) {
119             Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
120             return codec.deserialize(castedDefaultValue);
121         }
122         // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
123         // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
124         return null;
125     }
126
127     @Override
128     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
129         return yangIdentifier;
130     }
131
132     protected Codec<Object, Object> getValueCodec() {
133         return valueCodec;
134     }
135
136     @Override
137     public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
138         throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
139     }
140
141     @Override
142     public NodeCodecContext<?> get() {
143         return this;
144     }
145
146     Method getGetter() {
147         return getter;
148     }
149
150     @Override
151     public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
152             final List<YangInstanceIdentifier.PathArgument> builder) {
153         throw new IllegalArgumentException("Leaf does not have children");
154     }
155
156     @Override
157     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
158             final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
159         throw new UnsupportedOperationException("Leaves does not support caching codec.");
160     }
161
162     @Override
163     public Class<D> getBindingClass() {
164         throw new UnsupportedOperationException("Leaf does not have DataObject representation");
165     }
166
167     @Override
168     public NormalizedNode<?, ?> serialize(final D data) {
169         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
170     }
171
172     @Override
173     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
174         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
175     }
176
177     @Override
178     public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
179         throw new IllegalArgumentException("Leaf does not have children");
180     }
181
182     @Override
183     public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
184             final Class<E> childClass) {
185         throw new IllegalArgumentException("Leaf does not have children");
186     }
187
188     @Override
189     public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
190         throw new IllegalArgumentException("Leaf does not have children");
191     }
192
193     @Override
194     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
195         if (normalizedNode instanceof LeafNode<?>) {
196             return valueCodec.deserialize(normalizedNode.getValue());
197         }
198         if (normalizedNode instanceof LeafSetNode<?>) {
199             @SuppressWarnings("unchecked")
200             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
201             final List<Object> result = new ArrayList<>(domValues.size());
202             for (final LeafSetEntryNode<Object> valueNode : domValues) {
203                 result.add(valueCodec.deserialize(valueNode.getValue()));
204             }
205             return result;
206         }
207         return null;
208     }
209
210     @Override
211     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
212         Preconditions.checkArgument(getDomPathArgument().equals(arg));
213         return null;
214     }
215
216     @Override
217     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
218         return getDomPathArgument();
219     }
220
221     @Override
222     public DataSchemaNode getSchema() {
223         return schema;
224     }
225
226     /**
227      * Return the default value object.
228      *
229      * @return The default value object, or null if the default value is not defined.
230      */
231     @Nullable Object defaultObject() {
232         return defaultObject;
233     }
234 }