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