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
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
10 import com.google.common.annotations.Beta;
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;
17 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingNormalizedNodeCachingCodec;
22 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
23 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
25 import org.opendaylight.yangtools.concepts.Codec;
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;
43 public final class LeafNodeCodecContext<D extends TreeNode> 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 = Preconditions.checkNotNull(codec);
55 this.getter = Preconditions.checkNotNull(getter);
56 this.schema = Preconditions.checkNotNull(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 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
65 java.util.Optional<? extends Object> defaultValue = type.getDefaultValue();
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.get());
88 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
89 final String defaultValue, final SchemaContext schemaContext) {
90 final int prefixEndIndex = defaultValue.indexOf(':');
91 if (prefixEndIndex != -1) {
92 final String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
94 final Module module = schemaContext.findModule(schema.getQName().getModule()).get();
96 if (module.getPrefix().equals(defaultValuePrefix)) {
97 return codec.deserialize(QName.create(module.getQNameModule(),
98 defaultValue.substring(prefixEndIndex + 1)));
101 final Set<ModuleImport> imports = module.getImports();
102 for (final ModuleImport moduleImport : imports) {
103 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
104 final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
105 moduleImport.getRevision()).get();
106 return codec.deserialize(QName.create(importedModule.getQNameModule(),
107 defaultValue.substring(prefixEndIndex + 1)));
113 return codec.deserialize(QName.create(schema.getQName(), defaultValue));
116 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
117 final Object defaultValue) {
118 final TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
119 if (typeDefAwareCodec != null) {
120 final 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 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
130 return yangIdentifier;
133 public Codec<Object, Object> getValueCodec() {
139 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
140 throw new UnsupportedOperationException("Leaf can not be deserialized to TreeNode");
145 public NodeCodecContext<?> get() {
149 public Method getGetter() {
155 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
156 final List<YangInstanceIdentifier.PathArgument> builder) {
157 throw new IllegalArgumentException("Leaf does not have children");
162 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
163 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
164 throw new UnsupportedOperationException("Leaves does not support caching codec.");
169 public Class<D> getBindingClass() {
170 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
175 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
176 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
180 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
181 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
186 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
187 throw new IllegalArgumentException("Leaf does not have children");
191 public <E extends TreeNode> Optional<? extends BindingTreeNodeCodec<E>> possibleStreamChild(
192 @Nonnull final Class<E> childClass) {
193 throw new IllegalArgumentException("Leaf does not have children");
198 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
199 throw new IllegalArgumentException("Leaf does not have children");
203 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
204 if (normalizedNode instanceof LeafNode<?>) {
205 return valueCodec.deserialize(normalizedNode.getValue());
207 if (normalizedNode instanceof LeafSetNode<?>) {
208 @SuppressWarnings("unchecked")
209 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
210 final List<Object> result = new ArrayList<>(domValues.size());
211 for (final LeafSetEntryNode<Object> valueNode : domValues) {
212 result.add(valueCodec.deserialize(valueNode.getValue()));
219 @SuppressWarnings({ "rawtypes", "unchecked" })
221 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
222 Preconditions.checkArgument(getDomPathArgument().equals(arg));
226 @SuppressWarnings("rawtypes")
228 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
229 return getDomPathArgument();
234 public DataSchemaNode getSchema() {
239 * Return the default value object.
241 * @return The default value object, or null if the default value is not defined.
244 Object defaultObject() {
245 return defaultObject;