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.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
23 import org.opendaylight.yangtools.concepts.Codec;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
43 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
45 private final YangInstanceIdentifier.PathArgument yangIdentifier;
46 private final Codec<Object, Object> valueCodec;
47 private final Method getter;
48 private final DataSchemaNode schema;
49 private final Object defaultObject;
51 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
52 final SchemaContext schemaContext) {
53 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
54 this.valueCodec = requireNonNull(codec);
56 this.schema = requireNonNull(schema);
58 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
61 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
62 final SchemaContext schemaContext) {
63 if (schema instanceof LeafSchemaNode) {
64 java.util.Optional<? extends Object> defaultValue = ((LeafSchemaNode) schema).getType().getDefaultValue();
65 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
66 if (defaultValue.isPresent()) {
67 if (type instanceof IdentityrefTypeDefinition) {
68 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
70 return domValueFromString(codec, type, defaultValue.get());
73 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
74 type = type.getBaseType();
77 defaultValue = type.getDefaultValue();
78 if (defaultValue.isPresent()) {
79 if (type instanceof IdentityrefTypeDefinition) {
80 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
82 return domValueFromString(codec, type, defaultValue);
88 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
89 final String defaultValue, final SchemaContext schemaContext) {
90 int prefixEndIndex = defaultValue.indexOf(':');
92 if (prefixEndIndex != -1) {
93 String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
95 Module module = schemaContext.findModule(schema.getQName().getModule()).get();
96 if (module.getPrefix().equals(defaultValuePrefix)) {
97 qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
98 return codec.deserialize(qname);
101 Set<ModuleImport> imports = module.getImports();
102 for (ModuleImport moduleImport : imports) {
103 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
104 Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
105 moduleImport.getRevision()).get();
106 qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
107 return codec.deserialize(qname);
113 qname = QName.create(schema.getQName(), defaultValue);
114 return codec.deserialize(qname);
117 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
118 final Object defaultValue) {
119 TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
120 if (typeDefAwareCodec != null) {
121 Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
122 return codec.deserialize(castedDefaultValue);
124 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
125 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
130 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
131 return yangIdentifier;
134 protected Codec<Object, Object> getValueCodec() {
139 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
140 throw new UnsupportedOperationException("Leaf can not be deserialized to DataObject");
144 public NodeCodecContext<?> get() {
153 public BindingCodecTreeNode<?> bindingPathArgumentChild(final PathArgument arg,
154 final List<YangInstanceIdentifier.PathArgument> builder) {
155 throw new IllegalArgumentException("Leaf does not have children");
159 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
160 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
161 throw new UnsupportedOperationException("Leaves does not support caching codec.");
165 public Class<D> getBindingClass() {
166 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
170 public NormalizedNode<?, ?> serialize(final D data) {
171 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
175 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
176 throw new UnsupportedOperationException("Separete serialization of leaf node is not supported.");
180 public <E extends DataObject> BindingCodecTreeNode<E> streamChild(final Class<E> childClass) {
181 throw new IllegalArgumentException("Leaf does not have children");
185 public <E extends DataObject> Optional<? extends BindingCodecTreeNode<E>> possibleStreamChild(
186 final Class<E> childClass) {
187 throw new IllegalArgumentException("Leaf does not have children");
191 public BindingCodecTreeNode<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
192 throw new IllegalArgumentException("Leaf does not have children");
196 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
197 if (normalizedNode instanceof LeafNode<?>) {
198 return valueCodec.deserialize(normalizedNode.getValue());
200 if (normalizedNode instanceof LeafSetNode<?>) {
201 @SuppressWarnings("unchecked")
202 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
203 final List<Object> result = new ArrayList<>(domValues.size());
204 for (final LeafSetEntryNode<Object> valueNode : domValues) {
205 result.add(valueCodec.deserialize(valueNode.getValue()));
213 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
214 checkArgument(getDomPathArgument().equals(arg));
219 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
220 return getDomPathArgument();
224 public DataSchemaNode getSchema() {
229 public ChildAddressabilitySummary getChildAddressabilitySummary() {
230 return ChildAddressabilitySummary.UNADDRESSABLE;
234 * Return the default value object.
236 * @return The default value object, or null if the default value is not defined.
238 @Nullable Object defaultObject() {
239 return defaultObject;