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