Bump to odlparent-9.0.0/yangtools-7.0.1-SNAPSHOT
[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 java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
15 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
16 import org.opendaylight.yangtools.yang.binding.TypeObject;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28
29 class LeafNodeCodecContext extends ValueNodeCodecContext.WithCodec {
30     static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
31             implements BindingTypeObjectCodecTreeNode<T> {
32         private final @NonNull Class<T> bindingClass;
33
34         OfTypeObject(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
35                 final String getterName, final EffectiveModelContext schemaContext, final Class<T> bindingClass) {
36             super(schema, codec, getterName, schemaContext);
37             this.bindingClass = requireNonNull(bindingClass);
38         }
39
40         @Override
41         public Class<T> getBindingClass() {
42             return bindingClass;
43         }
44
45         @Override
46         public T deserialize(final NormalizedNode data) {
47             return bindingClass.cast(deserializeObject(data));
48         }
49
50         @Override
51         public NormalizedNode serialize(final T data) {
52             return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
53         }
54     }
55
56     LeafNodeCodecContext(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
57             final String getterName, final EffectiveModelContext schemaContext) {
58         super(schema, codec, getterName, createDefaultObject(schema, codec, schemaContext));
59     }
60
61     static LeafNodeCodecContext of(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
62             final String getterName, final Class<?> valueType, final EffectiveModelContext schemaContext) {
63         return TypeObject.class.isAssignableFrom(valueType)
64                 ? new OfTypeObject<>(schema, codec, getterName, schemaContext, valueType.asSubclass(TypeObject.class))
65                         : new LeafNodeCodecContext(schema, codec, getterName, schemaContext);
66     }
67
68     @Override
69     protected Object deserializeObject(final NormalizedNode normalizedNode) {
70         return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.body()) : null;
71     }
72
73     private static Object createDefaultObject(final LeafSchemaNode schema,
74                                               final IllegalArgumentCodec<Object, Object> codec,
75                                               final EffectiveModelContext 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);
81             }
82             return domValueFromString(codec, type, defaultValue.get());
83         }
84
85         while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
86             type = type.getBaseType();
87         }
88
89         defaultValue = type.getDefaultValue();
90         if (defaultValue.isPresent()) {
91             if (type instanceof IdentityrefTypeDefinition) {
92                 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
93             }
94             return domValueFromString(codec, type, defaultValue);
95         }
96         return null;
97     }
98
99     private static Object qnameDomValueFromString(final IllegalArgumentCodec<Object, Object> codec,
100                                                   final DataSchemaNode schema, final String defaultValue,
101                                                   final EffectiveModelContext schemaContext) {
102         int prefixEndIndex = defaultValue.indexOf(':');
103         QName qname;
104         if (prefixEndIndex != -1) {
105             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
106
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);
111             }
112
113             for (ModuleImport moduleImport : module.getImports()) {
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);
119                 }
120             }
121             return null;
122         }
123
124         qname = QName.create(schema.getQName(), defaultValue);
125         return codec.deserialize(qname);
126     }
127
128     private static Object domValueFromString(final IllegalArgumentCodec<Object, Object> codec,
129             final TypeDefinition<?> type, 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);
134         }
135         // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
136         //                 getDefaultValue() implementation.
137         return null;
138     }
139 }