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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
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;
19 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingNormalizedNodeCachingCodec;
24 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
25 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
26 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
27 import org.opendaylight.yangtools.concepts.Codec;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
45 public final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D> implements NodeContextSupplier {
47 private final YangInstanceIdentifier.PathArgument yangIdentifier;
48 private final Codec<Object, Object> valueCodec;
49 private final Method getter;
50 private final DataSchemaNode schema;
51 private final Object defaultObject;
53 LeafNodeCodecContext(final DataSchemaNode schema, final Codec<Object, Object> codec, final Method getter,
54 final SchemaContext schemaContext) {
55 this.yangIdentifier = new YangInstanceIdentifier.NodeIdentifier(schema.getQName());
56 this.valueCodec = requireNonNull(codec);
57 this.getter = requireNonNull(getter);
58 this.schema = requireNonNull(schema);
60 this.defaultObject = createDefaultObject(schema, valueCodec, schemaContext);
63 private static Object createDefaultObject(final DataSchemaNode schema, final Codec<Object, Object> codec,
64 final SchemaContext schemaContext) {
65 if (schema instanceof LeafSchemaNode) {
66 TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
67 Optional<? extends Object> defaultValue = type.getDefaultValue();
68 if (defaultValue.isPresent()) {
69 if (type instanceof IdentityrefTypeDefinition) {
70 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
72 return domValueFromString(codec, type, defaultValue.get());
75 while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
76 type = type.getBaseType();
79 defaultValue = type.getDefaultValue();
80 if (defaultValue.isPresent()) {
81 if (type instanceof IdentityrefTypeDefinition) {
82 return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
84 return domValueFromString(codec, type, defaultValue.get());
90 private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
91 final String defaultValue, final SchemaContext schemaContext) {
92 final int prefixEndIndex = defaultValue.indexOf(':');
93 if (prefixEndIndex != -1) {
94 final String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
96 final Module module = schemaContext.findModule(schema.getQName().getModule()).get();
98 if (module.getPrefix().equals(defaultValuePrefix)) {
99 return codec.deserialize(QName.create(module.getQNameModule(),
100 defaultValue.substring(prefixEndIndex + 1)));
103 final Set<ModuleImport> imports = module.getImports();
104 for (final ModuleImport moduleImport : imports) {
105 if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
106 final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
107 moduleImport.getRevision()).get();
108 return codec.deserialize(QName.create(importedModule.getQNameModule(),
109 defaultValue.substring(prefixEndIndex + 1)));
115 return codec.deserialize(QName.create(schema.getQName(), defaultValue));
118 private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
119 final Object defaultValue) {
120 final TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
121 if (typeDefAwareCodec != null) {
122 final Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);
123 return codec.deserialize(castedDefaultValue);
125 // FIXME: BUG-4647 Refactor / redesign this to throw hard error,
126 // once BUG-4638 is fixed and will provide proper getDefaultValue implementation.
131 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
132 return yangIdentifier;
135 public Codec<Object, Object> getValueCodec() {
141 public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
142 throw new UnsupportedOperationException("Leaf can not be deserialized to TreeNode");
147 public NodeCodecContext<?> get() {
151 public Method getGetter() {
157 public BindingTreeNodeCodec<?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
158 final List<YangInstanceIdentifier.PathArgument> builder) {
159 throw new IllegalArgumentException("Leaf does not have children");
164 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
165 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
166 throw new UnsupportedOperationException("Leaves does not support caching codec.");
171 public Class<D> getBindingClass() {
172 throw new UnsupportedOperationException("Leaf does not have DataObject representation");
177 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
178 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
182 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
183 throw new UnsupportedOperationException("Separate serialization of leaf node is not supported.");
188 public <E extends TreeNode> BindingTreeNodeCodec<E> streamChild(@Nonnull final Class<E> childClass) {
189 throw new IllegalArgumentException("Leaf does not have children");
193 public <E extends TreeNode> Optional<? extends BindingTreeNodeCodec<E>> possibleStreamChild(
194 @Nonnull final Class<E> childClass) {
195 throw new IllegalArgumentException("Leaf does not have children");
200 public BindingTreeNodeCodec<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument child) {
201 throw new IllegalArgumentException("Leaf does not have children");
205 protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
206 if (normalizedNode instanceof LeafNode<?>) {
207 return valueCodec.deserialize(normalizedNode.getValue());
209 if (normalizedNode instanceof LeafSetNode<?>) {
210 @SuppressWarnings("unchecked")
211 final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
212 final List<Object> result = new ArrayList<>(domValues.size());
213 for (final LeafSetEntryNode<Object> valueNode : domValues) {
214 result.add(valueCodec.deserialize(valueNode.getValue()));
221 @SuppressWarnings({ "rawtypes", "unchecked" })
223 public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
224 Preconditions.checkArgument(getDomPathArgument().equals(arg));
228 @SuppressWarnings("rawtypes")
230 public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
231 return getDomPathArgument();
236 public DataSchemaNode getSchema() {
241 * Return the default value object.
243 * @return The default value object, or null if the default value is not defined.
246 Object defaultObject() {
247 return defaultObject;