2 * Copyright (c) 2018 ZTE, 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
11 import static java.util.Objects.requireNonNull;
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;
33 public final class AnyxmlNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D>
34 implements NodeContextSupplier {
36 private final YangInstanceIdentifier.PathArgument yangIdentifier;
37 private final Codec<Object, Object> valueCodec;
38 private final Method getter;
39 private final DataSchemaNode schema;
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);
50 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
51 return yangIdentifier;
54 public Codec<Object, Object> getValueCodec() {
60 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
61 throw new UnsupportedOperationException("Anyxml can not be deserialized to TreeNode");
66 public NodeCodecContext<?> get() {
70 public Method getGetter() {
76 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
77 final List<YangInstanceIdentifier.PathArgument> builder) {
78 throw new IllegalArgumentException("Anyxml does not have children");
83 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
84 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
85 throw new UnsupportedOperationException("Anyxml does not support caching codec.");
90 public Class<D> getBindingClass() {
91 throw new UnsupportedOperationException("Anyxml does not have DataObject representation");
96 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
97 throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
101 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
102 throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
107 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
108 throw new IllegalArgumentException("Anyxml does not have children");
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");
119 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
120 throw new IllegalArgumentException("Anyxml does not have children");
124 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
125 if (normalizedNode instanceof AnyXmlNode) {
126 return valueCodec.deserialize(normalizedNode.getValue());
131 @SuppressWarnings({ "rawtypes", "unchecked" })
133 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
134 Preconditions.checkArgument(getDomPathArgument().equals(arg));
138 @SuppressWarnings("rawtypes")
140 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
141 return getDomPathArgument();
146 public DataSchemaNode getSchema() {