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;
37 public LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter) {
38 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
39 this.valueCodec = codec;
44 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
45 return (yangIdentifier);
48 protected Codec<Object, Object> getValueCodec() {
53 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
54 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
58 public NodeCodecContext<?> get() {
62 final Method getGetter() {
67 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
68 final List<YangInstanceIdentifier.PathArgument> builder) {
69 throw new IllegalArgumentException("Leaf does not have children");
73 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
74 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
75 throw new UnsupportedOperationException("Leaves does not support caching codec.");
79 public Class<D> getBindingClass() {
80 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
84 public NormalizedNode<?, ?> serialize(final D data) {
85 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
89 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
90 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
94 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
95 throw new IllegalArgumentException("Leaf does not have children");
99 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
100 final Class<E> childClass) {
101 throw new IllegalArgumentException("Leaf does not have children");
105 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
106 throw new IllegalArgumentException("Leaf does not have children");
110 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
111 if (normalizedNode instanceof LeafNode<?>) {
112 return valueCodec.deserialize(normalizedNode.getValue());
113 } else if(normalizedNode instanceof LeafSetNode<?>) {
114 @SuppressWarnings("unchecked")
115 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
116 final List<Object> result = new ArrayList<>(domValues.size());
117 for (final LeafSetEntryNode<Object> valueNode : domValues) {
118 result.add(valueCodec.deserialize(valueNode.getValue()));
126 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
127 Preconditions.checkArgument(getDomPathArgument().equals(arg));
132 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
133 return getDomPathArgument();