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 com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import javax.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
22 import org.opendaylight.yangtools.concepts.Codec;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
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.IdentitySchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IdentityEffectiveStatementImpl;
39 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
41 private final YangInstanceIdentifier.PathArgument yangIdentifier;
42 private final Codec<Object, Object> valueCodec;
43 private final Method getter;
44 private final DataSchemaNode schema;
45 private final Object defaultObject;
47 public LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter) {
48 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
49 this.valueCodec = Preconditions.checkNotNull(codec);
51 this.schema = Preconditions.checkNotNull(schema);
53 this.defaultObject = createDefaultObject(schema, valueCodec);
56 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec) {
57 if (schema instanceof LeafSchemaNode) {
58 Object defaultValue = ((LeafSchemaNode) schema).getDefault();
59 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
60 if (defaultValue != null) {
61 return domValueFromString(codec, type, defaultValue);
64 while (type.getBaseType() != null && type.getDefaultValue() == null) {
65 type = type.getBaseType();
68 defaultValue = type.getDefaultValue();
69 if (defaultValue != null) {
70 if (defaultValue instanceof Boolean) {
71 return codec.deserialize(defaultValue);
74 if (defaultValue instanceof IdentitySchemaNode) {
75 defaultValue = ((IdentityEffectiveStatementImpl) defaultValue).argument();
76 return codec.deserialize(defaultValue);
79 if (defaultValue instanceof ImmutableList) {
80 return codec.deserialize(ImmutableSet.copyOf((ImmutableList) defaultValue));
83 if (defaultValue instanceof List) {
84 return codec.deserialize(defaultValue);
86 return domValueFromString(codec, type, defaultValue);
93 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
94 Object defaultValue) {
95 TypeDefinitionAwareCodec typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
96 if (typeDefAwareCodec != null) {
97 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
98 return codec.deserialize(castedDefaultValue);
100 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
101 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
106 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
107 return yangIdentifier;
110 protected Codec<Object, Object> getValueCodec() {
115 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
116 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
120 public NodeCodecContext<?> get() {
124 final Method getGetter() {
129 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
130 final List<YangInstanceIdentifier.PathArgument> builder) {
131 throw new IllegalArgumentException("Leaf does not have children");
135 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
136 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
137 throw new UnsupportedOperationException("Leaves does not support caching codec.");
141 public Class<D> getBindingClass() {
142 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
146 public NormalizedNode<?, ?> serialize(final D data) {
147 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
151 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
152 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
156 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
157 throw new IllegalArgumentException("Leaf does not have children");
161 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
162 final Class<E> childClass) {
163 throw new IllegalArgumentException("Leaf does not have children");
167 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
168 throw new IllegalArgumentException("Leaf does not have children");
172 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
173 if (normalizedNode instanceof LeafNode<?>) {
174 return valueCodec.deserialize(normalizedNode.getValue());
176 if (normalizedNode instanceof LeafSetNode<?>) {
177 @SuppressWarnings("unchecked")
178 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
179 final List<Object> result = new ArrayList<>(domValues.size());
180 for (final LeafSetEntryNode<Object> valueNode : domValues) {
181 result.add(valueCodec.deserialize(valueNode.getValue()));
189 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
190 Preconditions.checkArgument(getDomPathArgument().equals(arg));
195 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
196 return getDomPathArgument();
200 public Object getSchema() {
205 * Return the default value object.
207 * @return The default value object, or null if the default value is not defined.
209 @Nullable Object defaultObject() {
210 return defaultObject;