2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
12 import java.lang.reflect.Method;
13 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.binding.TypeObject;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
31 class LeafNodeCodecContext extends ValueNodeCodecContext {
32 static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
33 implements BindingTypeObjectCodecTreeNode<T> {
34 private final @NonNull Class<T> bindingClass;
36 OfTypeObject(final LeafSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
37 final SchemaContext schemaContext, final Class<T> bindingClass) {
38 super(schema, codec, getter, schemaContext);
39 this.bindingClass = requireNonNull(bindingClass);
43 public Class<T> getBindingClass() {
48 public T deserialize(final NormalizedNode<?, ?> data) {
49 return bindingClass.cast(deserializeObject(data));
53 public NormalizedNode<?, ?> serialize(final T data) {
54 return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
58 LeafNodeCodecContext(final LeafSchemaNode schema, final Codec<Object, Object> codec,
59 final Method getter, final SchemaContext schemaContext) {
60 super(schema, codec, getter, createDefaultObject(schema, codec, schemaContext));
63 static LeafNodeCodecContext of(final LeafSchemaNode schema, final Codec<Object, Object> codec,
64 final Method getter, final Class<?> valueType, final SchemaContext schemaContext) {
65 return TypeObject.class.isAssignableFrom(valueType)
66 ? new OfTypeObject<>(schema, codec, getter, schemaContext, valueType.asSubclass(TypeObject.class))
67 : new LeafNodeCodecContext(schema, codec, getter, schemaContext);
71 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
72 return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.getValue()) : null;
75 private static Object createDefaultObject(final LeafSchemaNode schema, final Codec<Object, Object> codec,
76 final SchemaContext schemaContext) {
77 Optional<? extends Object> defaultValue = schema.getType().getDefaultValue();
78 TypeDefinition<?> type = schema.getType();
79 if (defaultValue.isPresent()) {
80 if (type instanceof IdentityrefTypeDefinition) {
81 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
83 return domValueFromString(codec, type, defaultValue.get());
86 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
87 type = type.getBaseType();
90 defaultValue = type.getDefaultValue();
91 if (defaultValue.isPresent()) {
92 if (type instanceof IdentityrefTypeDefinition) {
93 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
95 return domValueFromString(codec, type, defaultValue);
100 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
101 final String defaultValue, final SchemaContext schemaContext) {
102 int prefixEndIndex = defaultValue.indexOf(':');
104 if (prefixEndIndex != -1) {
105 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
107 Module module = schemaContext.findModule(schema.getQName().getModule()).get();
108 if (module.getPrefix().equals(defaultValuePrefix)) {
109 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
110 return codec.deserialize(qname);
113 Set<ModuleImport> imports = module.getImports();
114 for (ModuleImport moduleImport : imports) {
115 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
116 Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
117 moduleImport.getRevision()).get();
118 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
119 return codec.deserialize(qname);
125 qname = QName.create(schema.getQName(), defaultValue);
126 return codec.deserialize(qname);
129 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
130 final Object defaultValue) {
131 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
132 if (typeDefAwareCodec != null) {
133 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
134 return codec.deserialize(castedDefaultValue);
136 // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
137 // getDefaultValue() implementation.