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.mdsal.binding.dom.codec.impl;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.yangtools.concepts.Codec;
15 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
16 import org.opendaylight.yangtools.yang.common.Empty;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
23 * Value codec, which serializes / deserializes values from DOM simple values.
25 abstract class ValueTypeCodec implements Codec<Object, Object> {
27 private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
31 * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
32 * modules) they may have one static instance generated when first time needed.
34 interface SchemaUnawareCodec extends Codec<Object,Object> {
39 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
42 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
45 public Object serialize(final Object input) {
50 public Object deserialize(final Object input) {
55 public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
58 public Object serialize(final Object input) {
59 // Empty type has Empty value in NormalizedNode and Composite Node representation
60 return Empty.getInstance();
64 public Object deserialize(final Object input) {
65 /* Empty type has boolean.TRUE representation in Binding-aware world otherwise it is null / false.
66 * So when codec is triggered, empty leaf is present, that means we are safe to return true.
72 private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = () -> EMPTY_CODEC;
75 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
76 if (BindingReflections.isBindingClass(typeClz)) {
77 return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
79 return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
82 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
83 final Callable<? extends SchemaUnawareCodec> loader) {
85 return STATIC_CODECS.get(typeClz, loader);
86 } catch (ExecutionException e) {
87 throw new IllegalStateException(e);
91 private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
92 final TypeDefinition<?> def) {
94 TypeDefinition<?> rootType = def;
95 while (rootType.getBaseType() != null) {
96 rootType = rootType.getBaseType();
98 if (rootType instanceof EnumTypeDefinition) {
99 return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
100 } else if (rootType instanceof BitsTypeDefinition) {
101 return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
102 } else if (rootType instanceof EmptyTypeDefinition) {
105 return EncapsulatedValueCodec.loader(typeClz, def);
108 @SuppressWarnings("rawtypes")
109 static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
110 final Codec delegate) {
111 SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
112 EncapsulatedValueCodec.loader(typeClz, typeDef));
113 return new CompositeValueCodec(extractor, delegate);