2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
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;
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;
44 public final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D> implements NodeContextSupplier {
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;
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);
59 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
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);
71 return domValueFromString(codec, type, defaultValue);
74 while (type.getBaseType() != null && type.getDefaultValue() == null) {
75 type = type.getBaseType();
78 defaultValue = type.getDefaultValue();
79 if (defaultValue != null) {
80 if (type instanceof IdentityrefTypeDefinition) {
81 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
83 return domValueFromString(codec, type, defaultValue);
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);
96 final Module module = schemaContext.findModuleByNamespaceAndRevision(schema.getQName().getNamespace(),
97 schema.getQName().getRevision());
99 if (module.getPrefix().equals(defaultValuePrefix)) {
101 .deserialize(QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1)));
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)));
116 return codec.deserialize(QName.create(schema.getQName(), defaultValue));
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);
126 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
127 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
132 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
133 return yangIdentifier;
136 public Codec<Object, Object> getValueCodec() {
142 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
143 throw new UnsupportedOperationException("Leaf can not be deserialized to TreeNode");
148 public NodeCodecContext<?> get() {
152 public final Method getGetter() {
158 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
159 final List<YangInstanceIdentifier.PathArgument> builder) {
160 throw new IllegalArgumentException("Leaf does not have children");
165 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
166 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
167 throw new UnsupportedOperationException("Leaves does not support caching codec.");
172 public Class<D> getBindingClass() {
173 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
178 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
179 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
183 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
184 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
189 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
190 throw new IllegalArgumentException("Leaf does not have children");
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");
201 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
202 throw new IllegalArgumentException("Leaf does not have children");
206 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
207 if (normalizedNode instanceof LeafNode<?>) {
208 return valueCodec.deserialize(normalizedNode.getValue());
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()));
222 @SuppressWarnings({ "rawtypes", "unchecked" })
224 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
225 Preconditions.checkArgument(getDomPathArgument().equals(arg));
229 @SuppressWarnings("rawtypes")
231 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
232 return getDomPathArgument();
237 public DataSchemaNode getSchema() {
242 * Return the default value object.
244 * @return The default value object, or null if the default value is not defined.
247 Object defaultObject() {
248 return defaultObject;