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 org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
14 import org.opendaylight.yangtools.yang.binding.TypeObject;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
27 sealed class LeafNodeCodecContext extends ValueNodeCodecContext.WithCodec {
28 static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
29 implements BindingTypeObjectCodecTreeNode<T> {
30 private final @NonNull Class<T> bindingClass;
32 OfTypeObject(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
33 final EffectiveModelContext schemaContext, final Class<T> bindingClass) {
34 super(schema, codec, getterName, schemaContext);
35 this.bindingClass = requireNonNull(bindingClass);
39 public Class<T> getBindingClass() {
44 public T deserialize(final NormalizedNode data) {
45 return bindingClass.cast(deserializeObject(data));
49 public NormalizedNode serialize(final T data) {
50 return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
54 LeafNodeCodecContext(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
55 final EffectiveModelContext schemaContext) {
56 super(schema, codec, getterName, createDefaultObject(schema, codec, schemaContext));
59 static LeafNodeCodecContext of(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec,
60 final String getterName, final Class<?> valueType, final EffectiveModelContext schemaContext) {
61 return TypeObject.class.isAssignableFrom(valueType)
62 ? new OfTypeObject<>(schema, codec, getterName, schemaContext, valueType.asSubclass(TypeObject.class))
63 : new LeafNodeCodecContext(schema, codec, getterName, schemaContext);
67 protected Object deserializeObject(final NormalizedNode normalizedNode) {
68 return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.body()) : null;
71 private static Object createDefaultObject(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec,
72 final EffectiveModelContext schemaContext) {
73 var optDefaultValue = schema.getType().getDefaultValue();
74 TypeDefinition<?> type = schema.getType();
75 if (optDefaultValue.isPresent()) {
76 final var defaultValue = optDefaultValue.orElseThrow();
77 if (type instanceof IdentityrefTypeDefinition) {
78 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
80 return domValueFromString(codec, type, defaultValue);
83 while (type.getBaseType() != null && type.getDefaultValue().isEmpty()) {
84 type = type.getBaseType();
87 optDefaultValue = type.getDefaultValue();
88 if (optDefaultValue.isPresent()) {
89 final var defaultValue = optDefaultValue.orElseThrow();
90 if (type instanceof IdentityrefTypeDefinition) {
91 return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
93 return domValueFromString(codec, type, defaultValue);
98 private static Object qnameDomValueFromString(final ValueCodec<Object, Object> codec, final DataSchemaNode schema,
99 final String defaultValue, final EffectiveModelContext schemaContext) {
100 int prefixEndIndex = defaultValue.indexOf(':');
102 if (prefixEndIndex != -1) {
103 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
105 Module module = schemaContext.findModule(schema.getQName().getModule()).orElseThrow();
106 if (module.getPrefix().equals(defaultValuePrefix)) {
107 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
108 return codec.deserialize(qname);
111 for (ModuleImport moduleImport : module.getImports()) {
112 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
113 Module importedModule = schemaContext.findModule(moduleImport.getModuleName().getLocalName(),
114 moduleImport.getRevision()).orElseThrow();
115 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
116 return codec.deserialize(qname);
122 qname = QName.create(schema.getQName(), defaultValue);
123 return codec.deserialize(qname);
126 private static Object domValueFromString(final ValueCodec<Object, Object> codec, final TypeDefinition<?> type,
127 final Object defaultValue) {
128 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
129 if (typeDefAwareCodec != null) {
130 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
131 return codec.deserialize(castedDefaultValue);
133 // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
134 // getDefaultValue() implementation.