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;
10 import static org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeSpecificIdentifier;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableSet;
14 import java.lang.reflect.Method;
15 import java.util.LinkedHashSet;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.UnionValueOptionContext;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.BindingCodecContext;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ReflectionBasedCodec;
21 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
22 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.BaseYangTypes;
23 import org.opendaylight.yangtools.concepts.Codec;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
35 * Codec for serialize/deserialize union type.
39 public final class UnionTypeCodec extends ReflectionBasedCodec {
41 private final ImmutableSet<UnionValueOptionContext> typeCodecs;
43 private UnionTypeCodec(final Class<?> unionCls, final Set<UnionValueOptionContext> codecs) {
45 typeCodecs = ImmutableSet.copyOf(codecs);
49 * Loading union type codec for all subtypes of union.
52 * - binding class of union
54 * - type definition of union
55 * @param bindingCodecContext
56 * - binding codec context
59 public static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
60 final BindingCodecContext bindingCodecContext) {
62 final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
63 for (final TypeDefinition<?> subtype : unionType.getTypes()) {
64 if (subtype instanceof LeafrefTypeDefinition) {
65 addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
67 final Method valueGetter = unionCls.getMethod("get"
68 + normalizeSpecificIdentifier(subtype.getQName().getLocalName(), JavaIdentifier.CLASS));
69 final Class<?> valueType = valueGetter.getReturnType();
70 final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
71 values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
74 return new UnionTypeCodec(unionCls, values);
79 * Prepare codec for type from leaf's return type of leafref.
85 * @param bindingCodecContext
86 * - binding codec context
91 * @throws NoSuchMethodException when the getter method is not found
93 private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
94 final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
95 final TypeDefinition<?> subtype) throws NoSuchMethodException {
96 final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
97 final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
98 final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
99 // find schema node in schema context by xpath of leafref
100 final SchemaNode dataNode;
101 if (xpath.isAbsolute()) {
102 dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
104 dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
107 final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
109 // prepare name of type form return type of referenced leaf
110 final String typeName = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
111 .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode, null).getName();
113 // get method via reflection from generated code according to
114 // get_TypeName_Value method
115 final String method = normalizeSpecificIdentifier(new StringBuilder("get").append("_")
116 .append(typeName).append(unionCls.getSimpleName()).append("Value").toString(),
117 JavaIdentifier.METHOD);
118 final Method valueGetterParent = unionCls.getMethod(method);
119 final Class<?> returnType = valueGetterParent.getReturnType();
121 // prepare codec of union subtype according to return type of referenced
123 final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
124 values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
128 public Object deserialize(final Object input) {
129 for (final UnionValueOptionContext member : typeCodecs) {
130 final Object ret = member.deserializeUnion(input);
136 throw new IllegalArgumentException(
137 String.format("Failed to construct instance of %s for input %s", getTypeClass(), input));
141 public Object serialize(final Object input) {
143 for (final UnionValueOptionContext valCtx : typeCodecs) {
144 final Object domValue = valCtx.serialize(input);
145 if (domValue != null) {