81d9f3a609e75af35da4f76af9aabc51a4db1b0e
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / LeafNodeCodecContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.data.codec.impl;
9
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;
30
31 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
32
33     private final YangInstanceIdentifier.PathArgument yangIdentifier;
34     private final Codec<Object, Object> valueCodec;
35     private final Method getter;
36
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;
40         this.getter = getter;
41     }
42
43     @Override
44     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
45         return (yangIdentifier);
46     }
47
48     protected Codec<Object, Object> getValueCodec() {
49         return valueCodec;
50     }
51
52     @Override
53     public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
54         throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
55     }
56
57     @Override
58     public NodeCodecContext<?> get() {
59         return this;
60     }
61
62     final Method getGetter() {
63         return getter;
64     }
65
66     @Override
67     public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
68             final List<YangInstanceIdentifier.PathArgument> builder) {
69         throw new IllegalArgumentException("Leaf does not have children");
70     }
71
72     @Override
73     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
74             final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
75         throw new UnsupportedOperationException("Leaves does not support caching codec.");
76     }
77
78     @Override
79     public Class<D> getBindingClass() {
80         throw new UnsupportedOperationException("Leaf does not have DataObject representation");
81     }
82
83     @Override
84     public NormalizedNode<?, ?> serialize(final D data) {
85         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
86     }
87
88     @Override
89     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
90         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
91     }
92
93     @Override
94     public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
95         throw new IllegalArgumentException("Leaf does not have children");
96     }
97
98     @Override
99     public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
100             final Class<E> childClass) {
101         throw new IllegalArgumentException("Leaf does not have children");
102     }
103
104     @Override
105     public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
106         throw new IllegalArgumentException("Leaf does not have children");
107     }
108
109     @Override
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()));
119             }
120             return result;
121         }
122         return null;
123     }
124
125     @Override
126     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
127         Preconditions.checkArgument(getDomPathArgument().equals(arg));
128         return null;
129     }
130
131     @Override
132     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
133         return getDomPathArgument();
134     }
135
136 }