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