continue;
}
- final TypedDataSchemaNode typedSchema = (TypedDataSchemaNode) schema;
final ValueNodeCodecContext valueNode;
if (schema instanceof LeafSchemaNode) {
final LeafSchemaNode leafSchema = (LeafSchemaNode) schema;
final Class<?> valueType = method.getReturnType();
final Codec<Object, Object> codec = getCodec(valueType, leafSchema.getType());
- valueNode = new LeafNodeCodecContext(leafSchema, codec, method, context.getSchemaContext());
+ valueNode = LeafNodeCodecContext.of(leafSchema, codec, method, valueType,
+ context.getSchemaContext());
} else if (schema instanceof LeafListSchemaNode) {
final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(
method.getGenericReturnType());
final Codec<Object, Object> codec = getCodec(valueType, leafListSchema.getType());
valueNode = new LeafSetNodeCodecContext(leafListSchema, codec, method);
} else {
- throw new IllegalStateException("Unhandled typed schema " + typedSchema);
+ throw new IllegalStateException("Unhandled typed schema " + schema);
}
leaves.put(schema.getQName().getLocalName(), valueNode);
*/
package org.opendaylight.mdsal.binding.dom.codec.impl;
+import static java.util.Objects.requireNonNull;
+
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.yang.binding.TypeObject;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
-final class LeafNodeCodecContext extends ValueNodeCodecContext {
+class LeafNodeCodecContext extends ValueNodeCodecContext {
+ static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
+ implements BindingTypeObjectCodecTreeNode<T> {
+ private final @NonNull Class<T> bindingClass;
+
+ OfTypeObject(final LeafSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
+ final SchemaContext schemaContext, final Class<T> bindingClass) {
+ super(schema, codec, getter, schemaContext);
+ this.bindingClass = requireNonNull(bindingClass);
+ }
+
+ @Override
+ public Class<T> getBindingClass() {
+ return bindingClass;
+ }
+
+ @Override
+ public T deserialize(final NormalizedNode<?, ?> data) {
+ return bindingClass.cast(deserializeObject(data));
+ }
+
+ @Override
+ public NormalizedNode<?, ?> serialize(final T data) {
+ return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
+ }
+ }
+
LeafNodeCodecContext(final LeafSchemaNode schema, final Codec<Object, Object> codec,
- final Method getter, final SchemaContext schemaContext) {
+ final Method getter, final SchemaContext schemaContext) {
super(schema, codec, getter, createDefaultObject(schema, codec, schemaContext));
}
+ static LeafNodeCodecContext of(final LeafSchemaNode schema, final Codec<Object, Object> codec,
+ final Method getter, final Class<?> valueType, final SchemaContext schemaContext) {
+ return TypeObject.class.isAssignableFrom(valueType)
+ ? new OfTypeObject<>(schema, codec, getter, schemaContext, valueType.asSubclass(TypeObject.class))
+ : new LeafNodeCodecContext(schema, codec, getter, schemaContext);
+ }
+
@Override
protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.getValue()) : null;
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
package org.opendaylight.mdsal.binding.dom.codec.impl;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
import org.opendaylight.yangtools.yang.binding.TypeObject;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
/**
* A cache of NormalizedNodes corresponding to a particular TypeObject instantiation.
*/
-final class TypeObjectNormalizedNodeCache
- extends AbstractBindingNormalizedNodeCache<TypeObject, ValueNodeCodecContext> {
- TypeObjectNormalizedNodeCache(final ValueNodeCodecContext rootContext) {
+final class TypeObjectNormalizedNodeCache<T extends TypeObject,
+ C extends NodeCodecContext & BindingTypeObjectCodecTreeNode<T>>
+ extends AbstractBindingNormalizedNodeCache<T, C> {
+ TypeObjectNormalizedNodeCache(final C rootContext) {
super(rootContext);
}
@Override
- public NormalizedNode<?, ?> load(final TypeObject key) {
- return ImmutableNodes.leafNode(rootContext().getDomPathArgument(),
- rootContext().getValueCodec().serialize(key));
+ public NormalizedNode<?, ?> load(final T key) {
+ return rootContext().serialize(key);
}
}