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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableCollection;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
21 import org.opendaylight.yangtools.concepts.Codec;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
41 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
43 private final YangInstanceIdentifier.PathArgument yangIdentifier;
44 private final Codec<Object, Object> valueCodec;
45 private final Method getter;
46 private final DataSchemaNode schema;
47 private final Object defaultObject;
49 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
50 final SchemaContext schemaContext) {
51 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
52 this.valueCodec = Preconditions.checkNotNull(codec);
54 this.schema = Preconditions.checkNotNull(schema);
56 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
59 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
60 final SchemaContext schemaContext) {
61 if (schema instanceof LeafSchemaNode) {
62 java.util.Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
63 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
64 if (defaultValue.isPresent()) {
65 if (type instanceof IdentityrefTypeDefinition) {
66 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
68 return domValueFromString(codec, type, defaultValue.get());
71 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
72 type = type.getBaseType();
75 defaultValue = type.getDefaultValue();
76 if (defaultValue.isPresent()) {
77 if (type instanceof IdentityrefTypeDefinition) {
78 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
80 return domValueFromString(codec, type, defaultValue);
86 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
87 final String defaultValue, final SchemaContext schemaContext) {
88 int prefixEndIndex = defaultValue.indexOf(':');
90 if (prefixEndIndex != -1) {
91 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
93 Module module = schemaContext.findModule(schema.getQName().getModule()).get();
94 if (module.getPrefix().equals(defaultValuePrefix)) {
95 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
96 return codec.deserialize(qname);
99 Set<ModuleImport> imports = module.getImports();
100 for (ModuleImport moduleImport : imports) {
101 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
102 Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
103 moduleImport.getRevision()).get();
104 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
105 return codec.deserialize(qname);
111 qname = QName.create(schema.getQName(), defaultValue);
112 return codec.deserialize(qname);
115 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
116 final Object defaultValue) {
117 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
118 if (typeDefAwareCodec != null) {
119 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
120 return codec.deserialize(castedDefaultValue);
122 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
123 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
128 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
129 return yangIdentifier;
132 protected Codec<Object, Object> getValueCodec() {
137 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
138 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
142 public NodeCodecContext<?> get() {
151 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
152 final List<YangInstanceIdentifier.PathArgument> builder) {
153 throw new IllegalArgumentException("Leaf does not have children");
157 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
158 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
159 throw new UnsupportedOperationException("Leaves does not support caching codec.");
163 public Class<D> getBindingClass() {
164 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
168 public NormalizedNode<?, ?> serialize(final D data) {
169 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
173 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
174 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
178 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
179 throw new IllegalArgumentException("Leaf does not have children");
183 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
184 final Class<E> childClass) {
185 throw new IllegalArgumentException("Leaf does not have children");
189 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
190 throw new IllegalArgumentException("Leaf does not have children");
194 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
195 if (normalizedNode instanceof LeafNode<?>) {
196 return valueCodec.deserialize(normalizedNode.getValue());
198 if (normalizedNode instanceof LeafSetNode<?>) {
199 @SuppressWarnings("unchecked")
200 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
201 final List<Object> result = new ArrayList<>(domValues.size());
202 for (final LeafSetEntryNode<Object> valueNode : domValues) {
203 result.add(valueCodec.deserialize(valueNode.getValue()));
211 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
212 Preconditions.checkArgument(getDomPathArgument().equals(arg));
217 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
218 return getDomPathArgument();
222 public DataSchemaNode getSchema() {
227 public ChildAddressabilitySummary getChildAddressabilitySummary() {
228 return ChildAddressabilitySummary.UNADDRESSABLE;
232 * Return the default value object.
234 * @return The default value object, or null if the default value is not defined.
236 @Nullable Object defaultObject() {
237 return defaultObject;