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;
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;
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);
48 this.schema = requireNonNull(schema);
50 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
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);
62 return domValueFromString(codec, type, defaultValue.get());
65 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
66 type = type.getBaseType();
69 defaultValue = type.getDefaultValue();
70 if (defaultValue.isPresent()) {
71 if (type instanceof IdentityrefTypeDefinition) {
72 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
74 return domValueFromString(codec, type, defaultValue);
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(':');
84 if (prefixEndIndex != -1) {
85 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
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);
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);
105 qname = QName.create(schema.getQName(), defaultValue);
106 return codec.deserialize(qname);
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);
116 // FIXME: BUG-4647 Refactor / redesign this to throw hard error, once BUG-4638 is fixed and will provide proper
117 // getDefaultValue() implementation.
122 protected NodeIdentifier getDomPathArgument() {
123 return yangIdentifier;
126 protected Codec<Object, Object> getValueCodec() {
131 public NodeCodecContext get() {
140 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
141 if (normalizedNode instanceof LeafNode<?>) {
142 return valueCodec.deserialize(normalizedNode.getValue());
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()));
157 public TypedDataSchemaNode getSchema() {
162 Object defaultObject() {
163 return defaultObject;