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;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
20 import org.opendaylight.yangtools.concepts.Codec;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IdentityEffectiveStatementImpl;
38 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
40 private final YangInstanceIdentifier.PathArgument yangIdentifier;
41 private final Codec<Object, Object> valueCodec;
42 private final Method getter;
43 private final DataSchemaNode schema;
44 private final Object defaultObject;
46 public LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter) {
47 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
48 this.valueCodec = Preconditions.checkNotNull(codec);
50 this.schema = Preconditions.checkNotNull(schema);
52 this.defaultObject = createDefaultObject(schema, valueCodec);
55 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec) {
56 if (schema instanceof LeafSchemaNode) {
57 Object defaultValue = ((LeafSchemaNode) schema).getDefault();
58 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
59 if (defaultValue != null) {
60 return domValueFromString(codec, type, defaultValue);
63 while (type.getBaseType() != null && type.getDefaultValue() == null) {
64 type = type.getBaseType();
67 defaultValue = type.getDefaultValue();
68 if (defaultValue != null) {
69 if (defaultValue instanceof Boolean) {
70 return codec.deserialize(defaultValue);
73 if (defaultValue instanceof IdentitySchemaNode) {
74 defaultValue = ((IdentityEffectiveStatementImpl) defaultValue).argument();
75 return codec.deserialize(defaultValue);
78 if (defaultValue instanceof List) {
79 return codec.deserialize(defaultValue);
81 return domValueFromString(codec, type, defaultValue);
88 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
89 Object defaultValue) {
90 TypeDefinitionAwareCodec typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
91 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
92 return codec.deserialize(castedDefaultValue);
96 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
97 return yangIdentifier;
100 protected Codec<Object, Object> getValueCodec() {
105 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
106 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
110 public NodeCodecContext<?> get() {
114 final Method getGetter() {
119 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
120 final List<YangInstanceIdentifier.PathArgument> builder) {
121 throw new IllegalArgumentException("Leaf does not have children");
125 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
126 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
127 throw new UnsupportedOperationException("Leaves does not support caching codec.");
131 public Class<D> getBindingClass() {
132 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
136 public NormalizedNode<?, ?> serialize(final D data) {
137 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
141 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
142 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
146 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
147 throw new IllegalArgumentException("Leaf does not have children");
151 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
152 final Class<E> childClass) {
153 throw new IllegalArgumentException("Leaf does not have children");
157 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
158 throw new IllegalArgumentException("Leaf does not have children");
162 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
163 if (normalizedNode instanceof LeafNode<?>) {
164 return valueCodec.deserialize(normalizedNode.getValue());
166 if (normalizedNode instanceof LeafSetNode<?>) {
167 @SuppressWarnings("unchecked")
168 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
169 final List<Object> result = new ArrayList<>(domValues.size());
170 for (final LeafSetEntryNode<Object> valueNode : domValues) {
171 result.add(valueCodec.deserialize(valueNode.getValue()));
179 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
180 Preconditions.checkArgument(getDomPathArgument().equals(arg));
185 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
186 return getDomPathArgument();
190 public Object getSchema() {
195 * Return the default value object.
197 * @return The default value object, or null if the default value is not defined.
199 @Nullable Object defaultObject() {
200 return defaultObject;