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