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);
73 while (type.getBaseType() != null && type.getDefaultValue() == null) {
74 type = type.getBaseType();
77 defaultValue = type.getDefaultValue();
78 if (defaultValue != null) {
79 if (type instanceof IdentityrefTypeDefinition) {
80 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
82 return domValueFromString(codec, type, defaultValue);
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);
95 final Module module = schemaContext.findModuleByNamespaceAndRevision(schema.getQName().getNamespace(),
96 schema.getQName().getRevision());
98 if (module.getPrefix().equals(defaultValuePrefix)) {
100 .deserialize(QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1)));
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)));
115 return codec.deserialize(QName.create(schema.getQName(), defaultValue));
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);
125 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
126 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
131 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
132 return yangIdentifier;
135 public Codec<Object, Object> getValueCodec() {
141 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
142 throw new UnsupportedOperationException("Leaf can not be deserialized to TreeNode");
147 public NodeCodecContext<?> get() {
151 public Method getGetter() {
157 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
158 final List<YangInstanceIdentifier.PathArgument> builder) {
159 throw new IllegalArgumentException("Leaf does not have children");
164 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
165 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
166 throw new UnsupportedOperationException("Leaves does not support caching codec.");
171 public Class<D> getBindingClass() {
172 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
177 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
178 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
182 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
183 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
188 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
189 throw new IllegalArgumentException("Leaf does not have children");
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");
200 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
201 throw new IllegalArgumentException("Leaf does not have children");
205 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
206 if (normalizedNode instanceof LeafNode<?>) {
207 return valueCodec.deserialize(normalizedNode.getValue());
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()));
221 @SuppressWarnings({ "rawtypes", "unchecked" })
223 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
224 Preconditions.checkArgument(getDomPathArgument().equals(arg));
228 @SuppressWarnings("rawtypes")
230 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
231 return getDomPathArgument();
236 public DataSchemaNode getSchema() {
241 * Return the default value object.
243 * @return The default value object, or null if the default value is not defined.
246 Object defaultObject() {
247 return defaultObject;