Moved binding MD-SAL APIs to binding folder
[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     private final DataSchemaNode schema;
37
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;
41         this.getter = getter;
42         this.schema = schema;
43     }
44
45     @Override
46     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
47         return (yangIdentifier);
48     }
49
50     protected Codec<Object, Object> getValueCodec() {
51         return valueCodec;
52     }
53
54     @Override
55     public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
56         throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
57     }
58
59     @Override
60     public NodeCodecContext<?> get() {
61         return this;
62     }
63
64     final Method getGetter() {
65         return getter;
66     }
67
68     @Override
69     public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
70             final List<YangInstanceIdentifier.PathArgument> builder) {
71         throw new IllegalArgumentException("Leaf does not have children");
72     }
73
74     @Override
75     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
76             final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
77         throw new UnsupportedOperationException("Leaves does not support caching codec.");
78     }
79
80     @Override
81     public Class<D> getBindingClass() {
82         throw new UnsupportedOperationException("Leaf does not have DataObject representation");
83     }
84
85     @Override
86     public NormalizedNode<?, ?> serialize(final D data) {
87         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
88     }
89
90     @Override
91     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
92         throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
93     }
94
95     @Override
96     public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
97         throw new IllegalArgumentException("Leaf does not have children");
98     }
99
100     @Override
101     public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
102             final Class<E> childClass) {
103         throw new IllegalArgumentException("Leaf does not have children");
104     }
105
106     @Override
107     public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
108         throw new IllegalArgumentException("Leaf does not have children");
109     }
110
111     @Override
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()));
121             }
122             return result;
123         }
124         return null;
125     }
126
127     @Override
128     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
129         Preconditions.checkArgument(getDomPathArgument().equals(arg));
130         return null;
131     }
132
133     @Override
134     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
135         return getDomPathArgument();
136     }
137
138     @Override
139     public Object getSchema() {
140         return schema;
141     }
142
143 }