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.value;
11 import com.google.common.annotations.Beta;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
24 * Value codec, which serializes / de-serializes values from DOM simple values.
27 public abstract class ValueTypeCodec implements Codec<Object, Object> {
29 private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
33 * Marker interface for codecs, which functionality will not be
34 * affected by schema change (introduction of new YANG modules)
35 * they may have one static instance generated when
38 interface SchemaUnawareCodec extends Codec<Object,Object> {
42 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
43 * for base YANG types, representing numbers, binary and strings.
45 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
48 public Object serialize(final Object input) {
53 public Object deserialize(final Object input) {
58 public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
61 public Object serialize(final Object arg0) {
62 // Empty type has null value in NormalizedNode and Composite Node
68 public Object deserialize(final Object arg0) {
69 /* Empty type has boolean.TRUE representation in Binding-aware world
70 * otherwise it is null / false.
71 * So when codec is triggered, empty leaf is present, that means we
72 * are safe to return true.
78 private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = new Callable<SchemaUnawareCodec>() {
81 public SchemaUnawareCodec call() {
86 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
87 if (BindingReflections.isBindingClass(typeClz)) {
88 return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
90 return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
93 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
94 final Callable<? extends SchemaUnawareCodec> loader) {
96 return STATIC_CODECS.get(typeClz, loader);
97 } catch (final ExecutionException e) {
98 throw new IllegalStateException(e);
102 private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
103 final TypeDefinition<?> def) {
104 TypeDefinition<?> rootType = def;
105 while (rootType.getBaseType() != null) {
106 rootType = rootType.getBaseType();
108 if (rootType instanceof EnumTypeDefinition) {
109 return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
110 } else if (rootType instanceof BitsTypeDefinition) {
111 return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
112 } else if (rootType instanceof EmptyTypeDefinition) {
115 return EncapsulatedValueCodec.loader(typeClz);
118 @SuppressWarnings("rawtypes")
119 public static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
120 final SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
121 EncapsulatedValueCodec.loader(typeClz));
122 return new CompositeValueCodec(extractor, delegate);