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