906b29f1daaeb11bed0e86ae8114762243261472
[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 java.util.Set;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
16 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
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;
29
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;
34
35         OfTypeObject(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
36                 final String getterName, final SchemaContext schemaContext, final Class<T> bindingClass) {
37             super(schema, codec, getterName, schemaContext);
38             this.bindingClass = requireNonNull(bindingClass);
39         }
40
41         @Override
42         public Class<T> getBindingClass() {
43             return bindingClass;
44         }
45
46         @Override
47         public T deserialize(final NormalizedNode<?, ?> data) {
48             return bindingClass.cast(deserializeObject(data));
49         }
50
51         @Override
52         public NormalizedNode<?, ?> serialize(final T data) {
53             return ImmutableNodes.leafNode(getDomPathArgument(), getValueCodec().serialize(data));
54         }
55     }
56
57     LeafNodeCodecContext(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
58             final String getterName, final SchemaContext schemaContext) {
59         super(schema, codec, getterName, createDefaultObject(schema, codec, schemaContext));
60     }
61
62     static LeafNodeCodecContext of(final LeafSchemaNode schema, final IllegalArgumentCodec<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);
67     }
68
69     @Override
70     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
71         return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.getValue()) : null;
72     }
73
74     private static Object createDefaultObject(final LeafSchemaNode schema,
75                                               final IllegalArgumentCodec<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 IllegalArgumentCodec<Object, Object> codec,
101                                                   final DataSchemaNode schema, final String defaultValue,
102                                                   final SchemaContext schemaContext) {
103         int prefixEndIndex = defaultValue.indexOf(':');
104         QName qname;
105         if (prefixEndIndex != -1) {
106             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
107
108             Module module = schemaContext.findModule(schema.getQName().getModule()).get();
109             if (module.getPrefix().equals(defaultValuePrefix)) {
110                 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
111                 return codec.deserialize(qname);
112             }
113
114             Set<ModuleImport> imports = module.getImports();
115             for (ModuleImport moduleImport : imports) {
116                 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
117                     Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
118                         moduleImport.getRevision()).get();
119                     qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
120                     return codec.deserialize(qname);
121                 }
122             }
123             return null;
124         }
125
126         qname = QName.create(schema.getQName(), defaultValue);
127         return codec.deserialize(qname);
128     }
129
130     private static Object domValueFromString(final IllegalArgumentCodec<Object, Object> codec,
131             final TypeDefinition<?> type, final Object defaultValue) {
132         TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
133         if (typeDefAwareCodec != null) {
134             Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
135             return codec.deserialize(castedDefaultValue);
136         }
137         // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
138         //                 getDefaultValue() implementation.
139         return null;
140     }
141 }