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 org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
18 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeCachingCodec;
19 import org.opendaylight.yangtools.concepts.Codec;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
33 private final YangInstanceIdentifier.PathArgument yangIdentifier;
34 private final Codec<Object, Object> valueCodec;
35 private final Method getter;
36 private final DataSchemaNode schema;
38 public LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter) {
39 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
40 this.valueCodec = codec;
46 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
47 return (yangIdentifier);
50 protected Codec<Object, Object> getValueCodec() {
55 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
56 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
60 public NodeCodecContext<?> get() {
64 final Method getGetter() {
69 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
70 final List<YangInstanceIdentifier.PathArgument> builder) {
71 throw new IllegalArgumentException("Leaf does not have children");
75 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
76 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
77 throw new UnsupportedOperationException("Leaves does not support caching codec.");
81 public Class<D> getBindingClass() {
82 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
86 public NormalizedNode<?, ?> serialize(final D data) {
87 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
91 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
92 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
96 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
97 throw new IllegalArgumentException("Leaf does not have children");
101 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
102 final Class<E> childClass) {
103 throw new IllegalArgumentException("Leaf does not have children");
107 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
108 throw new IllegalArgumentException("Leaf does not have children");
112 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
113 if (normalizedNode instanceof LeafNode<?>) {
114 return valueCodec.deserialize(normalizedNode.getValue());
115 } else if(normalizedNode instanceof LeafSetNode<?>) {
116 @SuppressWarnings("unchecked")
117 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
118 final List<Object> result = new ArrayList<>(domValues.size());
119 for (final LeafSetEntryNode<Object> valueNode : domValues) {
120 result.add(valueCodec.deserialize(valueNode.getValue()));
128 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
129 Preconditions.checkArgument(getDomPathArgument().equals(arg));
134 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
135 return getDomPathArgument();
139 public Object getSchema() {