249c49cbb5226302f26f9dd5e9e1695400a5f76e
[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.lang.reflect.Method;
13 import java.util.Optional;
14 import java.util.Set;
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;
30
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;
35
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);
40         }
41
42         @Override
43         public Class<T> getBindingClass() {
44             return bindingClass;
45         }
46
47         @Override
48         public T deserialize(final NormalizedNode<?, ?> data) {
49             return bindingClass.cast(deserializeObject(data));
50         }
51
52         @Override
53         public NormalizedNode<?, ?> serialize(final T data) {
54             return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
55         }
56     }
57
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));
61     }
62
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);
68     }
69
70     @Override
71     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
72         return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.getValue()) : null;
73     }
74
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);
82             }
83             return domValueFromString(codec, type, defaultValue.get());
84         }
85
86         while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
87             type = type.getBaseType();
88         }
89
90         defaultValue = type.getDefaultValue();
91         if (defaultValue.isPresent()) {
92             if (type instanceof IdentityrefTypeDefinition) {
93                 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
94             }
95             return domValueFromString(codec, type, defaultValue);
96         }
97         return null;
98     }
99
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(':');
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             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);
120                 }
121             }
122             return null;
123         }
124
125         qname = QName.create(schema.getQName(), defaultValue);
126         return codec.deserialize(qname);
127     }
128
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);
135         }
136         // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
137         //                 getDefaultValue() implementation.
138         return null;
139     }
140 }