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