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.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
16 import org.opendaylight.yangtools.concepts.Codec;
17 import org.opendaylight.yangtools.yang.binding.TypeObject;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
30 class LeafNodeCodecContext extends ValueNodeCodecContext.WithCodec {
31 static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
32 implements BindingTypeObjectCodecTreeNode<T> {
33 private final @NonNull Class<T> bindingClass;
35 OfTypeObject(final LeafSchemaNode schema, final Codec<Object, Object> codec, final String getterName,
36 final SchemaContext schemaContext, final Class<T> bindingClass) {
37 super(schema, codec, getterName, schemaContext);
38 this.bindingClass = requireNonNull(bindingClass);
42 public Class<T> getBindingClass() {
47 public T deserialize(final NormalizedNode<?, ?> data) {
48 return bindingClass.cast(deserializeObject(data));
52 public NormalizedNode<?, ?> serialize(final T data) {
53 return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
57 LeafNodeCodecContext(final LeafSchemaNode schema, final Codec<Object, Object> codec,
58 final String getterName, final SchemaContext schemaContext) {
59 super(schema, codec, getterName, createDefaultObject(schema, codec, schemaContext));
62 static LeafNodeCodecContext of(final LeafSchemaNode schema, final Codec<Object, Object> codec,
63 final String getterName, final Class<?> valueType, final SchemaContext schemaContext) {
64 return TypeObject.class.isAssignableFrom(valueType)
65 ? new OfTypeObject<>(schema, codec, getterName, schemaContext, valueType.asSubclass(TypeObject.class))
66 : new LeafNodeCodecContext(schema, codec, getterName, schemaContext);
70 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
71 return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.getValue()) : null;
74 private static Object createDefaultObject(final LeafSchemaNode schema, final Codec<Object, Object> codec,
75 final SchemaContext schemaContext) {
76 Optional<? extends Object> defaultValue = schema.getType().getDefaultValue();
77 TypeDefinition<?> type = schema.getType();
78 if (defaultValue.isPresent()) {
79 if (type instanceof IdentityrefTypeDefinition) {
80 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
82 return domValueFromString(codec, type, defaultValue.get());
85 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
86 type = type.getBaseType();
89 defaultValue = type.getDefaultValue();
90 if (defaultValue.isPresent()) {
91 if (type instanceof IdentityrefTypeDefinition) {
92 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
94 return domValueFromString(codec, type, defaultValue);
99 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
100 final String defaultValue, final SchemaContext schemaContext) {
101 int prefixEndIndex = defaultValue.indexOf(':');
103 if (prefixEndIndex != -1) {
104 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
106 Module module = schemaContext.findModule(schema.getQName().getModule()).get();
107 if (module.getPrefix().equals(defaultValuePrefix)) {
108 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
109 return codec.deserialize(qname);
112 Set<ModuleImport> imports = module.getImports();
113 for (ModuleImport moduleImport : imports) {
114 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
115 Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
116 moduleImport.getRevision()).get();
117 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
118 return codec.deserialize(qname);
124 qname = QName.create(schema.getQName(), defaultValue);
125 return codec.deserialize(qname);
128 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
129 final Object defaultValue) {
130 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
131 if (typeDefAwareCodec != null) {
132 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
133 return codec.deserialize(castedDefaultValue);
135 // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
136 // getDefaultValue() implementation.