2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableCollection;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingNormalizedNodeCachingCodec;
23 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
25 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
26 import org.opendaylight.yangtools.concepts.Codec;
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;
44 public final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D> implements NodeContextSupplier {
46 private final YangInstanceIdentifier.PathArgument yangIdentifier;
47 private final Codec<Object, Object> valueCodec;
48 private final Method getter;
49 private final DataSchemaNode schema;
50 private final Object defaultObject;
52 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
53 final SchemaContext schemaContext) {
54 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
55 this.valueCodec = Preconditions.checkNotNull(codec);
56 this.getter = Preconditions.checkNotNull(getter);
57 this.schema = Preconditions.checkNotNull(schema);
59 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
62 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
63 final SchemaContext schemaContext) {
64 if (schema instanceof LeafSchemaNode) {
65 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
66 java.util.Optional<? extends Object> defaultValue = type.getDefaultValue();
67 if (defaultValue.isPresent()) {
68 if (type instanceof IdentityrefTypeDefinition) {
69 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
71 return domValueFromString(codec, type, defaultValue.get());
74 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
75 type = type.getBaseType();
78 defaultValue = type.getDefaultValue();
79 if (defaultValue.isPresent()) {
80 if (type instanceof IdentityrefTypeDefinition) {
81 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
83 return domValueFromString(codec, type, defaultValue.get());
89 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
90 final String defaultValue, final SchemaContext schemaContext) {
91 final int prefixEndIndex = defaultValue.indexOf(':');
92 if (prefixEndIndex != -1) {
93 final String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
95 final Module module = schemaContext.findModule(schema.getQName().getModule()).get();
97 if (module.getPrefix().equals(defaultValuePrefix)) {
98 return codec.deserialize(QName.create(module.getQNameModule(),
99 defaultValue.substring(prefixEndIndex + 1)));
102 final Set<ModuleImport> imports = module.getImports();
103 for (final ModuleImport moduleImport : imports) {
104 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
105 final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
106 moduleImport.getRevision()).get();
107 return codec.deserialize(QName.create(importedModule.getQNameModule(),
108 defaultValue.substring(prefixEndIndex + 1)));
114 return codec.deserialize(QName.create(schema.getQName(), defaultValue));
117 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
118 final Object defaultValue) {
119 final TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
120 if (typeDefAwareCodec != null) {
121 final 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 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
131 return yangIdentifier;
134 public Codec<Object, Object> getValueCodec() {
140 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
141 throw new UnsupportedOperationException("Leaf can not be deserialized to TreeNode");
146 public NodeCodecContext<?> get() {
150 public Method getGetter() {
156 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
157 final List<YangInstanceIdentifier.PathArgument> builder) {
158 throw new IllegalArgumentException("Leaf does not have children");
163 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
164 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
165 throw new UnsupportedOperationException("Leaves does not support caching codec.");
170 public Class<D> getBindingClass() {
171 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
176 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
177 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
181 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
182 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
187 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
188 throw new IllegalArgumentException("Leaf does not have children");
192 public <E extends TreeNode> Optional<? extends BindingTreeNodeCodec<E>> possibleStreamChild(
193 @Nonnull final Class<E> childClass) {
194 throw new IllegalArgumentException("Leaf does not have children");
199 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
200 throw new IllegalArgumentException("Leaf does not have children");
204 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
205 if (normalizedNode instanceof LeafNode<?>) {
206 return valueCodec.deserialize(normalizedNode.getValue());
208 if (normalizedNode instanceof LeafSetNode<?>) {
209 @SuppressWarnings("unchecked")
210 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
211 final List<Object> result = new ArrayList<>(domValues.size());
212 for (final LeafSetEntryNode<Object> valueNode : domValues) {
213 result.add(valueCodec.deserialize(valueNode.getValue()));
220 @SuppressWarnings({ "rawtypes", "unchecked" })
222 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
223 Preconditions.checkArgument(getDomPathArgument().equals(arg));
227 @SuppressWarnings("rawtypes")
229 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
230 return getDomPathArgument();
235 public DataSchemaNode getSchema() {
240 * Return the default value object.
242 * @return The default value object, or null if the default value is not defined.
245 Object defaultObject() {
246 return defaultObject;