bb3250c87e31755f475521d9356a0d13c3ff861e
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / LeafNodeCodecContext.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
26
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;
31
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);
36         }
37
38         @Override
39         public Class<T> getBindingClass() {
40             return bindingClass;
41         }
42
43         @Override
44         public T deserialize(final NormalizedNode data) {
45             return bindingClass.cast(deserializeObject(data));
46         }
47
48         @Override
49         public NormalizedNode serialize(final T data) {
50             return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
51         }
52     }
53
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));
57     }
58
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);
64     }
65
66     @Override
67     protected Object deserializeObject(final NormalizedNode normalizedNode) {
68         return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.body()) : null;
69     }
70
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);
79             }
80             return domValueFromString(codec, type, defaultValue);
81         }
82
83         while (type.getBaseType() != null && type.getDefaultValue().isEmpty()) {
84             type = type.getBaseType();
85         }
86
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);
92             }
93             return domValueFromString(codec, type, defaultValue);
94         }
95         return null;
96     }
97
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(':');
101         QName qname;
102         if (prefixEndIndex != -1) {
103             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
104
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);
109             }
110
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);
117                 }
118             }
119             return null;
120         }
121
122         qname = QName.create(schema.getQName(), defaultValue);
123         return codec.deserialize(qname);
124     }
125
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);
132         }
133         // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
134         //                 getDefaultValue() implementation.
135         return null;
136     }
137 }