Mass-migrate to java.util.Optional
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / base / AnyxmlNodeCodecContext.java
1 /*
2  * Copyright (c) 2018 ZTE, 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableCollection;
14 import java.lang.reflect.Method;
15 import java.util.List;
16 import java.util.Optional;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingNormalizedNodeCachingCodec;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
20 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
21 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
22 import org.opendaylight.yangtools.concepts.Codec;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 @Beta
31 public final class AnyxmlNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D>
32         implements NodeContextSupplier {
33
34     private final YangInstanceIdentifier.PathArgument yangIdentifier;
35     private final Codec<Object, Object> valueCodec;
36     private final Method getter;
37     private final DataSchemaNode schema;
38
39     AnyxmlNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
40                            final SchemaContext schemaContext) {
41         this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
42         this.valueCodec = Preconditions.checkNotNull(codec);
43         this.getter = Preconditions.checkNotNull(getter);
44         this.schema = Preconditions.checkNotNull(schema);
45     }
46
47     @Override
48     public YangInstanceIdentifier.PathArgument getDomPathArgument() {
49         return yangIdentifier;
50     }
51
52     public Codec<Object, Object> getValueCodec() {
53         return valueCodec;
54     }
55
56     @Nonnull
57     @Override
58     public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
59         throw new UnsupportedOperationException("Anyxml can not be deserialized to TreeNode");
60     }
61
62     @Nonnull
63     @Override
64     public NodeCodecContext<?> get() {
65         return this;
66     }
67
68     public Method getGetter() {
69         return getter;
70     }
71
72     @Nonnull
73     @Override
74     public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
75             final List<YangInstanceIdentifier.PathArgument> builder) {
76         throw new IllegalArgumentException("Anyxml does not have children");
77     }
78
79     @Nonnull
80     @Override
81     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
82             @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
83         throw new UnsupportedOperationException("Anyxml does not support caching codec.");
84     }
85
86     @Nonnull
87     @Override
88     public Class<D> getBindingClass() {
89         throw new UnsupportedOperationException("Anyxml does not have DataObject representation");
90     }
91
92     @Nonnull
93     @Override
94     public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
95         throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
96     }
97
98     @Override
99     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
100         throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
101     }
102
103     @Nonnull
104     @Override
105     public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
106         throw new IllegalArgumentException("Anyxml does not have children");
107     }
108
109     @Override
110     public <E extends TreeNode> Optional<? extends BindingTreeNodeCodec<E>> possibleStreamChild(
111             @Nonnull final Class<E> childClass) {
112         throw new IllegalArgumentException("Anyxml does not have children");
113     }
114
115     @Nonnull
116     @Override
117     public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
118         throw new IllegalArgumentException("Anyxml does not have children");
119     }
120
121     @Override
122     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
123         if (normalizedNode instanceof AnyXmlNode) {
124             return valueCodec.deserialize(normalizedNode.getValue());
125         }
126         return null;
127     }
128
129     @SuppressWarnings({ "rawtypes", "unchecked" })
130     @Override
131     public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
132         Preconditions.checkArgument(getDomPathArgument().equals(arg));
133         return null;
134     }
135
136     @SuppressWarnings("rawtypes")
137     @Override
138     public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
139         return getDomPathArgument();
140     }
141
142     @Nonnull
143     @Override
144     public DataSchemaNode getSchema() {
145         return schema;
146     }
147 }