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.yangtools.binding.data.codec.impl;
10 import com.google.common.collect.ImmutableSet;
11 import java.lang.reflect.Method;
12 import java.util.LinkedHashSet;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.yangtools.concepts.Codec;
16 import org.opendaylight.yangtools.sal.binding.yang.types.BaseYangTypes;
17 import org.opendaylight.yangtools.yang.binding.BindingMapping;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
28 final class UnionTypeCodec extends ReflectionBasedCodec {
29 private final ImmutableSet<UnionValueOptionContext> typeCodecs;
31 private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
33 this.typeCodecs = ImmutableSet.copyOf(codecs);
36 static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
37 final BindingCodecContext bindingCodecContext) {
39 final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
40 for (final TypeDefinition<?> subtype : unionType.getTypes()) {
41 if (subtype instanceof LeafrefTypeDefinition) {
42 addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
44 final Method valueGetter =
45 unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
46 final Class<?> valueType = valueGetter.getReturnType();
47 final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
49 values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
52 return new UnionTypeCodec(unionCls, values);
57 * Prepare codec for type from leaf's return type of leafref.
63 * @param bindingCodecContext
64 * - binding codec context
69 * @throws NoSuchMethodException
71 private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
72 final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
73 final TypeDefinition<?> subtype) throws NoSuchMethodException {
74 final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
75 final Module module = schemaContext.findModuleByNamespaceAndRevision(
76 subtype.getQName().getNamespace(), subtype.getQName().getRevision());
77 final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
78 // find schema node in schema context by xpath of leafref
79 final SchemaNode dataNode;
80 if (xpath.isAbsolute()) {
81 dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
83 dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module,
86 final String className = BindingMapping.getClassName(unionCls.getSimpleName());
87 final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
89 // prepare name of type form return type of referenced leaf
90 final String typeName = BindingMapping.getClassName(BaseYangTypes.BASE_YANG_TYPES_PROVIDER
91 .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
93 // get method via reflection from generated code according to
94 // get_TypeName_Value method
95 final Method valueGetterParent = unionCls.getMethod(
96 new StringBuilder("get").append(typeName).append(className).append("Value").toString());
97 final Class<?> returnType = valueGetterParent.getReturnType();
99 // prepare codec of union subtype according to return type of referenced
101 final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
102 values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
106 public Object deserialize(final Object input) {
107 for (final UnionValueOptionContext member : this.typeCodecs) {
108 final Object ret = member.deserializeUnion(input);
114 throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
115 getTypeClass(), input));
119 public Object serialize(final Object input) {
121 for (final UnionValueOptionContext valCtx : this.typeCodecs) {
122 final Object domValue = valCtx.serialize(input);
123 if (domValue != null) {