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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableCollection;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Optional;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
22 import org.opendaylight.yangtools.concepts.Codec;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
42 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
44 private final YangInstanceIdentifier.PathArgument yangIdentifier;
45 private final Codec<Object, Object> valueCodec;
46 private final Method getter;
47 private final DataSchemaNode schema;
48 private final Object defaultObject;
50 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
51 final SchemaContext schemaContext) {
52 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
53 this.valueCodec = requireNonNull(codec);
55 this.schema = requireNonNull(schema);
57 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
60 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
61 final SchemaContext schemaContext) {
62 if (schema instanceof LeafSchemaNode) {
63 Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
64 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
65 if (defaultValue.isPresent()) {
66 if (type instanceof IdentityrefTypeDefinition) {
67 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
69 return domValueFromString(codec, type, defaultValue.get());
72 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
73 type = type.getBaseType();
76 defaultValue = type.getDefaultValue();
77 if (defaultValue.isPresent()) {
78 if (type instanceof IdentityrefTypeDefinition) {
79 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
81 return domValueFromString(codec, type, defaultValue);
87 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
88 final String defaultValue, final SchemaContext schemaContext) {
89 int prefixEndIndex = defaultValue.indexOf(':');
91 if (prefixEndIndex != -1) {
92 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
94 Module module = schemaContext.findModule(schema.getQName().getModule()).get();
95 if (module.getPrefix().equals(defaultValuePrefix)) {
96 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
97 return codec.deserialize(qname);
100 Set<ModuleImport> imports = module.getImports();
101 for (ModuleImport moduleImport : imports) {
102 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
103 Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
104 moduleImport.getRevision()).get();
105 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
106 return codec.deserialize(qname);
112 qname = QName.create(schema.getQName(), defaultValue);
113 return codec.deserialize(qname);
116 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
117 final Object defaultValue) {
118 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
119 if (typeDefAwareCodec != null) {
120 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
121 return codec.deserialize(castedDefaultValue);
123 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
124 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
129 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
130 return yangIdentifier;
133 protected Codec<Object, Object> getValueCodec() {
138 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
139 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
143 public NodeCodecContext<?> get() {
152 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
153 final List<YangInstanceIdentifier.PathArgument> builder) {
154 throw new IllegalArgumentException("Leaf does not have children");
158 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
159 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
160 throw new UnsupportedOperationException("Leaves does not support caching codec.");
164 public Class<D> getBindingClass() {
165 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
169 public NormalizedNode<?, ?> serialize(final D data) {
170 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
174 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
175 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
179 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
180 throw new IllegalArgumentException("Leaf does not have children");
184 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
185 final Class<E> childClass) {
186 throw new IllegalArgumentException("Leaf does not have children");
190 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
191 throw new IllegalArgumentException("Leaf does not have children");
195 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
196 if (normalizedNode instanceof LeafNode<?>) {
197 return valueCodec.deserialize(normalizedNode.getValue());
199 if (normalizedNode instanceof LeafSetNode<?>) {
200 @SuppressWarnings("unchecked")
201 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
202 final List<Object> result = new ArrayList<>(domValues.size());
203 for (final LeafSetEntryNode<Object> valueNode : domValues) {
204 result.add(valueCodec.deserialize(valueNode.getValue()));
212 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
213 checkArgument(getDomPathArgument().equals(arg));
218 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
219 return getDomPathArgument();
223 public DataSchemaNode getSchema() {
228 public ChildAddressabilitySummary getChildAddressabilitySummary() {
229 return ChildAddressabilitySummary.UNADDRESSABLE;
233 Object defaultObject() {
234 return defaultObject;