2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
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;
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;
36 * Abstract base class for atomic nodes. These are nodes which are not decomposed in the Binding Specification, such
37 * as LeafNodes and LeafSetNodes.
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;
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);
52 this.schema = requireNonNull(schema);
54 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
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);
66 return domValueFromString(codec, type, defaultValue.get());
69 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
70 type = type.getBaseType();
73 defaultValue = type.getDefaultValue();
74 if (defaultValue.isPresent()) {
75 if (type instanceof IdentityrefTypeDefinition) {
76 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
78 return domValueFromString(codec, type, defaultValue);
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(':');
88 if (prefixEndIndex != -1) {
89 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
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);
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);
109 qname = QName.create(schema.getQName(), defaultValue);
110 return codec.deserialize(qname);
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);
120 // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
121 // getDefaultValue() implementation.
126 protected NodeIdentifier getDomPathArgument() {
127 return yangIdentifier;
130 protected Codec<Object, Object> getValueCodec() {
135 public NodeCodecContext get() {
144 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
145 if (normalizedNode instanceof LeafNode<?>) {
146 return valueCodec.deserialize(normalizedNode.getValue());
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()));
161 public TypedDataSchemaNode getSchema() {
166 Object defaultObject() {
167 return defaultObject;