2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.yangtools.binding.data.codec.impl;
10 import com.google.common.base.Optional;
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;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
21 import org.opendaylight.yangtools.concepts.Codec;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
41 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
43 private final YangInstanceIdentifier.PathArgument yangIdentifier;
44 private final Codec<Object, Object> valueCodec;
45 private final Method getter;
46 private final DataSchemaNode schema;
47 private final Object defaultObject;
49 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
50 final SchemaContext schemaContext) {
51 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
52 this.valueCodec = Preconditions.checkNotNull(codec);
54 this.schema = Preconditions.checkNotNull(schema);
56 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
59 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
60 final SchemaContext schemaContext) {
61 if (schema instanceof LeafSchemaNode) {
62 Object defaultValue = ((LeafSchemaNode) schema).getDefault();
63 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
64 if (defaultValue != null) {
65 if (type instanceof IdentityrefTypeDefinition) {
66 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
68 return domValueFromString(codec, type, defaultValue);
71 while (type.getBaseType() != null && type.getDefaultValue() == null) {
72 type = type.getBaseType();
75 defaultValue = type.getDefaultValue();
76 if (defaultValue != null) {
77 if (type instanceof IdentityrefTypeDefinition) {
78 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
80 return domValueFromString(codec, type, defaultValue);
87 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
88 final String defaultValue, final SchemaContext schemaContext) {
89 int prefixEndIndex = defaultValue.indexOf(':');
91 if (prefixEndIndex != -1) {
92 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
94 Module module = schemaContext.findModuleByNamespaceAndRevision(schema.getQName().getNamespace(),
95 schema.getQName().getRevision());
97 if (module.getPrefix().equals(defaultValuePrefix)) {
98 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
99 return codec.deserialize(qname);
101 Set<ModuleImport> imports = module.getImports();
102 for (ModuleImport moduleImport : imports) {
103 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
104 Module importedModule = schemaContext.findModuleByName(moduleImport.getModuleName(),
105 moduleImport.getRevision());
106 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
107 return codec.deserialize(qname);
114 qname = QName.create(schema.getQName(), defaultValue);
115 return codec.deserialize(qname);
118 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
119 Object defaultValue) {
120 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
121 if (typeDefAwareCodec != null) {
122 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 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
132 return yangIdentifier;
135 protected Codec<Object, Object> getValueCodec() {
140 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
141 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
145 public NodeCodecContext<?> get() {
149 final Method getGetter() {
154 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
155 final List<YangInstanceIdentifier.PathArgument> builder) {
156 throw new IllegalArgumentException("Leaf does not have children");
160 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
161 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
162 throw new UnsupportedOperationException("Leaves does not support caching codec.");
166 public Class<D> getBindingClass() {
167 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
171 public NormalizedNode<?, ?> serialize(final D data) {
172 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
176 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
177 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
181 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
182 throw new IllegalArgumentException("Leaf does not have children");
186 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
187 final Class<E> childClass) {
188 throw new IllegalArgumentException("Leaf does not have children");
192 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
193 throw new IllegalArgumentException("Leaf does not have children");
197 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
198 if (normalizedNode instanceof LeafNode<?>) {
199 return valueCodec.deserialize(normalizedNode.getValue());
201 if (normalizedNode instanceof LeafSetNode<?>) {
202 @SuppressWarnings("unchecked")
203 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
204 final List<Object> result = new ArrayList<>(domValues.size());
205 for (final LeafSetEntryNode<Object> valueNode : domValues) {
206 result.add(valueCodec.deserialize(valueNode.getValue()));
214 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
215 Preconditions.checkArgument(getDomPathArgument().equals(arg));
220 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
221 return getDomPathArgument();
225 public DataSchemaNode getSchema() {
230 * Return the default value object.
232 * @return The default value object, or null if the default value is not defined.
234 @Nullable Object defaultObject() {
235 return defaultObject;