Allow BindingNormalizedNodeCachingCodec on any BindingObject
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / LeafNodeCodecContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.yangtools.concepts.Codec;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34
35 // FIXME: MDSAL-436: this class should be specialized for Leaf and LeafSet
36 final class LeafNodeCodecContext extends NodeCodecContext implements NodeContextSupplier {
37     private final NodeIdentifier yangIdentifier;
38     private final Codec<Object, Object> valueCodec;
39     private final Method getter;
40     private final TypedDataSchemaNode schema;
41     private final Object defaultObject;
42
43     LeafNodeCodecContext(final TypedDataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
44                 final SchemaContext schemaContext) {
45         this.yangIdentifier = NodeIdentifier.create(schema.getQName());
46         this.valueCodec = requireNonNull(codec);
47         this.getter = getter;
48         this.schema = requireNonNull(schema);
49
50         this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
51     }
52
53     private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
54                                               final SchemaContext schemaContext) {
55         if (schema instanceof LeafSchemaNode) {
56             Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
57             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
58             if (defaultValue.isPresent()) {
59                 if (type instanceof IdentityrefTypeDefinition) {
60                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
61                 }
62                 return domValueFromString(codec, type, defaultValue.get());
63             }
64
65             while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
66                 type = type.getBaseType();
67             }
68
69             defaultValue = type.getDefaultValue();
70             if (defaultValue.isPresent()) {
71                 if (type instanceof IdentityrefTypeDefinition) {
72                     return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
73                 }
74                 return domValueFromString(codec, type, defaultValue);
75             }
76         }
77         return null;
78     }
79
80     private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
81                                                   final String defaultValue, final SchemaContext schemaContext) {
82         int prefixEndIndex = defaultValue.indexOf(':');
83         QName qname;
84         if (prefixEndIndex != -1) {
85             String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
86
87             Module module = schemaContext.findModule(schema.getQName().getModule()).get();
88             if (module.getPrefix().equals(defaultValuePrefix)) {
89                 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
90                 return codec.deserialize(qname);
91             }
92
93             Set<ModuleImport> imports = module.getImports();
94             for (ModuleImport moduleImport : imports) {
95                 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
96                     Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
97                         moduleImport.getRevision()).get();
98                     qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
99                     return codec.deserialize(qname);
100                 }
101             }
102             return null;
103         }
104
105         qname = QName.create(schema.getQName(), defaultValue);
106         return codec.deserialize(qname);
107     }
108
109     private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
110             final Object defaultValue) {
111         TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
112         if (typeDefAwareCodec != null) {
113             Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
114             return codec.deserialize(castedDefaultValue);
115         }
116         // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
117         //                 getDefaultValue() implementation.
118         return null;
119     }
120
121     @Override
122     protected NodeIdentifier getDomPathArgument() {
123         return yangIdentifier;
124     }
125
126     protected Codec<Object, Object> getValueCodec() {
127         return valueCodec;
128     }
129
130     @Override
131     public NodeCodecContext get() {
132         return this;
133     }
134
135     Method getGetter() {
136         return getter;
137     }
138
139     @Override
140     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
141         if (normalizedNode instanceof LeafNode<?>) {
142             return valueCodec.deserialize(normalizedNode.getValue());
143         }
144         if (normalizedNode instanceof LeafSetNode<?>) {
145             @SuppressWarnings("unchecked")
146             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
147             final List<Object> result = new ArrayList<>(domValues.size());
148             for (final LeafSetEntryNode<Object> valueNode : domValues) {
149                 result.add(valueCodec.deserialize(valueNode.getValue()));
150             }
151             return result;
152         }
153         return null;
154     }
155
156     @Override
157     public TypedDataSchemaNode getSchema() {
158         return schema;
159     }
160
161     @Override
162     Object defaultObject() {
163         return defaultObject;
164     }
165 }