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 com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableCollection;
15 import java.lang.reflect.Method;
16 import java.util.List;
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;
31 public final class AnyxmlNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D>
32 implements NodeContextSupplier {
34 private final YangInstanceIdentifier.PathArgument yangIdentifier;
35 private final Codec<Object, Object> valueCodec;
36 private final Method getter;
37 private final DataSchemaNode schema;
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);
48 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
49 return yangIdentifier;
52 public Codec<Object, Object> getValueCodec() {
58 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
59 throw new UnsupportedOperationException("Anyxml can not be deserialized to TreeNode");
64 public NodeCodecContext<?> get() {
68 public Method getGetter() {
74 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
75 final List<YangInstanceIdentifier.PathArgument> builder) {
76 throw new IllegalArgumentException("Anyxml does not have children");
81 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
82 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
83 throw new UnsupportedOperationException("Anyxml does not support caching codec.");
88 public Class<D> getBindingClass() {
89 throw new UnsupportedOperationException("Anyxml does not have DataObject representation");
94 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
95 throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
99 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
100 throw new UnsupportedOperationException("Separate serialization of Anyxml node is not supported.");
105 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
106 throw new IllegalArgumentException("Anyxml does not have children");
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");
117 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
118 throw new IllegalArgumentException("Anyxml does not have children");
122 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
123 if (normalizedNode instanceof AnyXmlNode) {
124 return valueCodec.deserialize(normalizedNode.getValue());
129 @SuppressWarnings({ "rawtypes", "unchecked" })
131 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
132 Preconditions.checkArgument(getDomPathArgument().equals(arg));
136 @SuppressWarnings("rawtypes")
138 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
139 return getDomPathArgument();
144 public DataSchemaNode getSchema() {